我正在尝试在 bash 中创建一个脚本,以便为 Domoticz 中的 3 个虚拟传感器重新发布 1 条 MQTT 消息。我在为 mossquito_pub 命令创建消息部分时遇到了麻烦
#!/bin/bash
# This script subscribes to a MQTT topic using mosquitto_sub.
# On each message received, you can execute whatever you want.
while true # Keep an infinite loop to reconnect when connection lost/broker unavailable
do
mosquitto_sub -h "192.168.1.30" -t "tele/tasmota_D875F9/SENSOR" | while read -r payload
do
# Here is the callback to execute whenever you receive a message:
str="${payload}"
voltage1= echo $str | cut -f 22 -d ','| tr -dc '0-9'
echo $voltage1
mosquitto_pub -h 192.168.1.30 -p 1883 -t 'domoticz/in' -m '{"idx" : 33, "nvalue" : 0, "svalue" : "$voltage1" }'
voltage2= echo $str | cut -f 23 -d ','| tr -dc '0-9'
echo $voltage2
mosquitto_pub -h 192.168.1.30 -p 1883 -t 'domoticz/in' -m '{"idx" : 34, "nvalue" : 0, "svalue" : "$voltage2" }'
voltage3= echo $str | cut -f 24 -d ','| tr -dc '0-9'
echo $voltage3
mosquitto_pub -h 192.168.1.30 -p 1883 -t 'domoticz/in' -m '{"idx" : 35, "nvalue" : 0, "svalue" : "$voltage3" }'
done
sleep 10 # Wait 10 seconds until reconnection
done # & # Discomment the & to run in background (but you should rather run THIS script in background)
sleep 10 # Wait 10 seconds until reconnection
done # & # Discomment the & to run in background (but you should rather run THIS script in background)
目前,终端回显输出为:
230
231
233
传递的信息:
2020-12-01 13:51:27.952 MQTT: Topic: domoticz/in, Message: {"idx" : 33, "nvalue" : 0, "svalue" : "$voltage1" }
2020-12-01 13:51:28.057 MQTT: Topic: domoticz/in, Message: {"idx" : 34, "nvalue" : 0, "svalue" : "$voltage2" }
2020-12-01 13:51:28.161 MQTT: Topic: domoticz/in, Message: {"idx" : 35, "nvalue" : 0, "svalue" : "$voltage3" }
我需要将变量voltage1
, voltage2
,voltage3
替换为它们的值,因为它们在终端回显输出中看到,包括前面和后面的引号。
请帮我。