Escape characters in "executecommandline" for double-quotes

2,013 views
Skip to first unread message

Andreas G.

unread,
Jan 23, 2015, 2:56:48 PM1/23/15
to ope...@googlegroups.com
I am trying to create a rule using the "ExecuteCommandLine" action, but I seem to have trouble with my double-quotes.

So, in order to test the general way of working with "ExecuteCommandLine" and escape characters, I simply tried opening an existing file with the standard editor.

In my Linux shell, the following command runs flawlessly and opens the existing file (I explicitly use double-quotes for the file-name to test escape characters):
pluma "/etc/init.d/openhab"

In my openHAB rule, I consequently put:
executeCommandLine("pluma \"/etc/init.d/openhab\"")

According to the logfile, the command is correctly executed and the escape characters seem to work:
2015-01-23 19:49:09.295 [INFO ] [g.openhab.io.net.exec.ExecUtil] - executed commandLine 'pluma "/etc/init.d/openhab"'

However, instead of opening the existing file "/etc/init.d/openhab", the editor will open a new and empty file called openhab" (yes, with a quotation mark in the end), supposedly located in </usr/share/openhab/" /etc/init.d>.

Moreover, I get the exact same result (new file in funny directory) by typing the following command into my command line shell (inital command + openHAB escape characters):
pluma \"/etc/init.d/openhab\"

I therefore conclude that the string inside my openHAB rule is directly sent to my command line, including escape character <\">. What am I doing wrong? Is this a bug? I would appreciate your help. I am running openHAB 1.6.1 on a Linux MINT distribution.

PS: I do not want to put my command into a script and then execute the script via openHAB. In my eyes, this defeats the purpose of this function.

Andreas G.

unread,
Jan 23, 2015, 4:54:46 PM1/23/15
to ope...@googlegroups.com
I think that I have found an answer myself. I looked a bit inside the source code of ExecuteCommandLine which essentially uses the JAVA command "Runtime.getRuntime().exec" to execute the string in the shell.

Google came up with the following post when it came to "Runtime.getRuntime().exec" and "escape characters":
http://stackoverflow.com/questions/5969724/java-runtime-exec-fails-to-escape-characters-properly

In talks about passing a "string [] array" instead of a "string" to pass a command and its parameters. This rang a bell, because the ExecuteCommandLine code speaks of a "@@" delimiter:

 * Executes <code>commandLine</code>. Sometimes (especially observed on
 * MacOS) the commandLine isn't executed properly. In that cases another
 * exec-method is to be used. To accomplish this please use the special
 * delimiter '<code>@@</code>'. If <code>commandLine</code> contains this
 * delimiter it is split into a String[] array and the special exec-method
 * is used

Therefore, I tested the following code in my rule and it works fine:
executeCommandLine("pluma@@/etc/init.d/openhab")

The resulting logfile shows:
2015-01-23 22:30:21.620 [INFO ] [g.openhab.io.net.exec.ExecUtil] - executed commandLine '[pluma, /etc/init.d/openhab]'

Eventually, this allowed me to successfully pass the more complicated command to send a WhatsApp message:
executeCommandLine("/opt/yowsup-master/yowsup-cli@@demos@@-s@@49xxxxxxxxxx@@Yoh! What's up?@@-c@@/opt/yowsup-master/config.yowsup")

Logfile output:
2015-01-23 22:51:43.183 [INFO ] [g.openhab.io.net.exec.ExecUtil] - executed commandLine '[/opt/yowsup-master/yowsup-cli, demos, -s, 49xxxxxxxxxx, Yoh! What's up?, -c, /opt/yowsup-master/config.yowsup]'

It is the equivalent of the following command line (which absolutely requires double-quotes in command line to indicate the "text message"):
/opt/yowsup-master/yowsup-cli demos -s 49xxxxxxxxxx "Yoh! What's up?" -c /opt/yowsup-master/config.yowsup
Reply all
Reply to author
Forward
0 new messages