Page 1 of 1

start script with parameters containing comma's

Posted: Monday 17 April 2023 13:55
by GammaKappa
I use a simple bash script to send out an MQTT message :

Code: Select all

#!/bin/bash
# Generic MQTT send script
# $1 = topic
# $2 = message
mosquitto_pub -h 192.168.1.xxx -t $1 -m $2 
I call this script with the Blockly element Start script ... with parameter(s) ... where the parameters are the MQTT's topic and message

This works fine if I call it from Blockly with a simple argument like TestMessage
If the message argument contains comma's and is enclosed within double quotes, the script returns error 256
clip3.jpg
clip3.jpg (66.94 KiB) Viewed 1717 times

How do I correctly pass an argument that contains comma's ?

Re: start script with parameters containing comma's

Posted: Monday 17 April 2023 14:24
by waltervl
I do not know for blockly.
But when you have a switch called "Server Offline" You can also use the On and Off action commands in the device (edit button).
See wiki https://www.domoticz.com/wiki/Managing_ ... Off_Action

With bash you normally do not need quotes unless you expect spaces in the argument.
If the command needs quotes you can put them in the bash script:

Code: Select all

mosquitto_pub -h 192.168.1.xxx -t $1 -m "$2"

Re: start script with parameters containing comma's

Posted: Monday 17 April 2023 15:26
by GammaKappa
Some further testing resulted in :

When I run the bash script from the command line with as second argument :
1,2,3 -> no problem, the MQTT message is sent out as 1,2,3
"1,2,3" -> no problem, the MQTT message is sent out as 1,2,3

When the script is called from Blockly with as second argument :
1,2,3 -> only the first digit is passed, the MQTT message is sent out as 1
"1,2,3" -> all digits are passed but the MQTT message is sent out as 1 2 3 (without the comma's)

So now the question is how do I keep the comma's in the message when calling the script from Blockly ?

Re: start script with parameters containing comma's

Posted: Monday 17 April 2023 15:56
by waltervl
Comma's seem intentially to be removed from parameter field due to further processing.... see source code https://github.com/domoticz/domoticz/bl ... er.js#L434

As a workaround you perhaps can use the On/Off action script fields on a device as mentioned before.

Re: start script with parameters containing comma's

Posted: Monday 17 April 2023 17:14
by GammaKappa
Thanks for the tip about why the comma's are being removed ! This put me on the road to a solution.

As a workaround I now set the second argument as 1_2_3 (no comma's but underscores).
This argument is passed by Blockly to the bash script as 1_2_3 as expected (and not secretly removing characters).

In the bash script I replace the underscores again by comma's, and the MQTT message is sent as 1,2,3 .

The workaround you suggested by using the On/Off switch to start the script might also work, but in my case the particular switch also sets other actions not mentioned in the example above. Keeping all the actions in one place (Blockly) makes it easier for me to manage.

Re: start script with parameters containing comma's

Posted: Monday 17 April 2023 21:15
by waltervl
Yes, that also will work ;)