0

I am trying to create a script in bash in order to republish 1 MQTT message for 3 virtual sensors in Domoticz. I am running into a trouble while creating message part for mossquito_pub command

#!/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)

Currently, terminal echo output is:

230
231
233

Message transmitted:

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" }

I need variables voltage1, voltage2, voltage3 to be replaced by their values as they are seen in terminal echo output including quotation marks in front and right after.

Please help me.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • 1
    The single quotes protect the string inside them so it is forwarded verbatim. You have to use double quotes, and then escape any literal double quotes and other shell metacharacters (dollar sign, backtick, backslash) which would be protected by the single quotes. – tripleee Dec 01 '20 at 13:02
  • The line `voltage1= echo $str | cut -f 22 -d ','| tr -dc '0-9'` does not do what you think it does. It does not assign a value to `voltage1` but runs `echo` in an environment that contains the variable `voltage1` without any value. In order to assign a value to `voltage1` you have to remove the spaces around the `=` sign and to wrap the commands whose output needs to be captured in `$(...)`, like this: `voltage1=$(echo $str | cut -f 22 -d ','| tr -dc '0-9')`. – axiac Dec 01 '20 at 13:08
  • The substring extraction looks really painful anyway. Maybe try something like `mosquitto_sub -h "192.168.1.30" -t "tele/tasmota_D875F9/SENSOR" | cut -d, -f22-24 | tr -dc ,0-9 | while IFS=, read -r voltage1 voltage2 voltage3` – tripleee Dec 01 '20 at 13:08
  • You are great! Thanks. Using properly escape "\" worked properly. 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"\" : 36, "\"nvalue"\" : 0, "\"svalue"\" : "\"$voltage1"\" }" – Viktoria Peťková-Tuchtejová Dec 01 '20 at 14:01

0 Answers0