mqtt 메시지에 복잡한 bash 문자열을 넣으려고 합니다.

mqtt 메시지에 복잡한 bash 문자열을 넣으려고 합니다.

다음 문자열(간단한 숫자)의 출력을 맞추려고 합니다.

df -hl | grep '/dev/mapper/4tb' | awk '{;percent+=$5;} END{print percent}' | column -t

다음 mqtt 메시지를 입력하세요.

mosquitto_pub -h 192.168.1.1 -p 1883 -u user -P password -t disk_usage/4tb -m *[here sould go the output of the previous command]*

다음 스크립트와 다양한 변형(다른 따옴표 및 대괄호 등)을 시도했지만 여전히 이해할 수 없습니다.

#!/bin/sh
var1='{df -hl | grep '/dev/mapper/4tb' | awk '{;percent+=$5;} END{print percent}' | column -t}'
echo $var1

mosquitto_pub -h 192.168.1.65 -p 1883 -u mqtt -P mqtt_password -t GC01SRVR/disk_usage -m "{\"Content\": $var1}"

답변1

echo "static string 1 $(cmd # this is replaced by the stdout output of cmd) string 2"

echo "abc $(date --rfc-3339=seconds) xyz"
abc 2021-11-19 20:12:18+01:00 xyz

그래서 당신의 경우에는

echo "mosquitto_pub -h 192.168.1.1 -p 1883 -u user -P password -t disk_usage/4tb -m $(df -hl | grep '/dev/mapper/4tb' | awk '{;percent+=$5;} END{print percent}')"

또는

cmd_output="$(df -hl | grep '/dev/mapper/4tb' | awk '{;percent+=$5;} END{print percent}')"

static_string="mosquitto_pub -h 192.168.1.1 -p 1883 -u user -P password -t disk_usage/4tb -m "

echo "${static_string}${cmd_output}"

관련 정보