Hi i am an odd scenario
if i send below command manually in terminal it works well
root@VPRT:/home/root# mosquitto_pub -h localhost -p 1883 -q 1 -d -t test -i localClientID -m '2020-07-14 15:03:27'
But same when sent from the Bash script I get error
Error: Unknown option '15:03:27''.
my shell script is :
#!/bin/bash
mqttcmd="mosquitto_pub -h localhost -p 1883 -q 1 -d -t test -i localClientID -m "
dateformat="%Y-%m-%d %H:%M:%S"
function my_date {
date "+${dateformat}"
}
while true; do
today=$(my_date)
echo "today : " $today
mystring="$mqttcmd"
mystring+="'"
mystring+="$today"
mystring+="'"
#print
echo ${mystring}
#publish
${mystring}
sleep 5
done
if I send the same command that is PRINTED from the script it works but from the shell I get error. error only occurs with date format. if I send any other text from script it works. if I add the space between the date and time I get error.
i am confused as the same date with space works if manually sent
As suggested by @Gordon Davisson, it worked.
You can use variables as part of commands (but put double-quotes around them), but don't put commands in variables. For your script, remove the mqttcmd and mystring variables, and just run mosquitto_pub -h localhost -p 1883 -q 1 -d -t test -i localClientID -m "$today" – Gordon Davisson