[SOLVED] Double quotes, etc. in executeCommandLine

openhab 2.5 on Ubuntu 18.04:

In Linux I’m able to send an SMS using Pushbullet with the following command:

curl --header 'Access-Token: o.XXxxxXXXXxxxxXXXXxxx' --header 'Content-Type: application/json' --data-binary '{"push":{"conversation_iden": "+336XXXXXXX", "message": "SOME TEXT TO SEND WITH SMS ON THE MOBILE", "package_name": "com.pushbullet.android", "source_user_iden": "xxxXXXxxXX", "target_device_iden": "xxXXxxxXXXxxxxXXX", "type": "messaging_extension_reply"},"type":"push"}' --request POST https://api.pushbullet.com/v2/ephemerals

I would like to use executeCommandLine to run the same command in a rule. Does anyone have a good idea on how I can escape the double quotation marks (just adding \ does not work)? The double quotation marks are lost if I use @@, and I’m not quite sure where to put them in any case…

I have tried:

val StringBuilder myCommand = new StringBuilder
myCommand.append(“curl --header ‘Access-Token: o.xxxXXXXxxxxXXXXX’ --header ‘Content-Type: application/json’ --data-binary '{”)
myCommand.append(’“push”:{“conversation_iden”: “+336XXXXXXXX”, “message”: “’)
myCommand.append(’”, “package_name”: “com.pushbullet.android”, “source_user_iden”: “xxxXXXX”, “target_device_iden”: “xxxXXXXxxxxxXXXX”, “type”: “messaging_extension_reply”},“type”:“push”}’)
myCommand.append("’ --request POST https://api.pushbullet.com/v2/ephemerals")
val result = executeCommandLine(myCommand.toString)

The log shows:

executed commandLine ‘curl --header ‘Access-Token: o.xxxxXXXX’ --header ‘Content-Type: application/json’ --data-binary ‘{“push”:{“conversation_iden”: “+336XXXXXX”, “message”: “THEMESSAGE”, “package_name”: “com.pushbullet.android”, “source_user_iden”: “xxxxxxx”, “target_device_iden”: “xxxxx”, “type”: “messaging_extension_reply”},“type”:“push”}’ --request POST https://api.pushbullet.com/v2/ephemerals

but the command does not work. If I paste in the command in putty, it works.

What’s the content of that result variable afterwards?

Try without any single quotes, just double quotes and escapes. You may need to escape some escapes though (\\\).

val StringBuilder myCommand = new StringBuilder
myCommand.append("curl@@--header@@Access-Token: o.xxxXXXXxxxxXXXXX@@--header@@Content-Type: application/json@@--data-binary@@")
myCommand.append("{\"push\":{\"conversation_iden\": \"+336XXXXXXXX\", \"message\":")
myCommand.append(", \"package_name\": \"com.pushbullet.android\", \"source_user_iden\": \"xxxXXXX\", \"target_device_iden\": \"xxxXXXXxxxxxXXXX\", \"type\": \"messaging_extension_reply\"},\"type\":\"push\"}")
val result = executeCommandLine(myCommand.toString)

Let me know if this works. I have a PR for the documentation that should hopefully clarify this issue.

1 Like


That did it! A thousand thanks! Merry Christmas to all, and a special thought to JimT

1 Like

Here’s a cleaner code:

String command = new String.join("@@",
"--header", "Access-Token: o.xxxXXXXxxxxXXXXX",
"--header", "Content-Type: application/json",
"{\"push\":{\"conversation_iden\": \"+336XXXXXXXX\", \"message\": \"THEMESSAGE\", \"package_name\": \"com.pushbullet.android\", \"source_user_iden\": \"xxxXXXX\", \"target_device_iden\": \"xxxXXXXxxxxxXXXX\", \"type\": \"messaging_extension_reply\"},\"type\":\"push\"}",
"--request", "POST",
val result = executeCommandLine(command)

If you can use a json builder, it would look even neater without the double quote escapes.

I don’t know off the top of my head which json library is available in OpenHAB without having to install any additional package. Maybe someone else can jump in and provide this info?