How to use "executeCommandLine" correctly

I have the following Rule, but Version 1 of this rule always gives me a wrong result:

rule "test execute Command"
when
    Item Dummy_3 changed to ON
then
//  Version 1 - funktioniert nicht - ich kann den Grund nicht finden
    var pingresult = executeCommandLine(Duration.ofSeconds(20),"/bin/ping","-c5","-W1", "192.168.178.75", ">", "/dev/null", "&&", "/bin/echo", 'online', "||", "/bin/echo", 'offline')  // String in einer Variablen zwischenspeichern
    logInfo("execute-Version 1","String - pingresult: {}", pingresult)                             // Log  pingresult
//  Version 2 - Der Shell-Befehl verpackt in einem Bash-Script, dann klappt's
    pingresult = executeCommandLine(Duration.ofSeconds(10),"/bin/bash","/etc/openhab/scripts/ping_test.sh")  // String in einer Variablen zwischenspeichern
    logInfo("execute-Version 2","String - pingresult: {}", pingresult)                             // Log  pingresult
//  Version 3 - Shell-Kommando - einfach
    pingresult = executeCommandLine(Duration.ofSeconds(2),"/bin/ls", "-alF")  // String in einer Variablen zwischenspeichern
    logInfo("execute-Version 3","String - pingresult: {}", pingresult)                             // Log  pingresult

/* Das Bash-Script (ping_test.sh)- als Beispiel - Einfacher geht's mit dem Network-Binding ;)

#!/bin/bash
INPUT=$(/bin/ping -c5 -W1 192.168.178.99 >/dev/null && /bin/echo 'online' || /bin/echo 'offline')
echo $INPUT

*/

end

As a result I get in the log:

2021-06-27 13:30:37.266 [INFO ] [.core.model.script.execute-Version 1] - String - pingresult: ping: offline: Der Name oder der Dienst ist nicht bekannt
2021-06-27 13:30:41.404 [INFO ] [.core.model.script.execute-Version 2] - String - pingresult: online
2021-06-27 13:30:41.434 [INFO ] [.core.model.script.execute-Version 3] - String - pingresult: insgesamt 56
drwxrwxr-x+ 12 openhab openhab  4096 Jun  3 00:05 ./
drwxr-xr-x  31 root    root     4096 Dez 26  2020 ../
drwxrwxr-x+  5 openhab openhab  4096 Jun 25 13:50 cache/
drwx------+  3 openhab openhab  4096 Dez 30 14:05 .config/
drwxrwxr-x+  5 openhab openhab  4096 Jun  2 23:35 config/
drwxrwxr-x+  3 openhab openhab 12288 Jun  2 23:33 etc/
drwxrwxr-x+  3 openhab openhab  4096 Jan 26 00:23 jsondb/
drwxrwxr-x+  2 openhab openhab  4096 Jun  3 00:05 kar/
drwxrwxr-x+  2 openhab openhab  4096 Jun  5 22:40 .karaf/
drwxrwxr-x+  5 openhab openhab  4096 Dez 26  2020 persistence/
drwxrwxr-x+  2 openhab openhab  4096 Dez 26  2020 secrets/
drwxrwxr-x+ 11 openhab openhab  4096 Jun 25 13:53 tmp/

When I use the command in the console everything works fine:

hab3@hostHAB3:~ $ /bin/ping -c5 -W1 192.168.178.99 > /dev/null && /bin/echo 'online' || /bin/echo 'offline'
online
h

Does anyone know what I’m doing wrong in my command:

var pingresult = executeCommandLine(Duration.ofSeconds(20),"/bin/ping","-c5","-W1", "192.168.178.75", ">", "/dev/null", "&&", "/bin/echo", 'online', "||", "/bin/echo", 'offline')

Every help or hint is welcomed and thx in advance.

Cheers,
Peter

You can not use redirection, pipeing with starting multiple programs in executeCommandLine.
You can start only 1 program with its parameters.
Perhaps the function name executeCommandLine is misleading, it should be executeProgram or executeProcess.

If you want redirection and pipeing with starting multiple programs you have to use a script as you tried or use bash with the command line as direct parameter:

var pingresult = executeCommandLine(Duration.ofSeconds(5), "/bin/bash", "-c", "/bin/ping -c5 -W1 192.168.178.99 > /dev/null && /bin/echo 'online' || /bin/echo 'offline'")
3 Likes

thx, that’s it.
Cheers,
Peter

Just one more short question! What does the parameter “-c” in the “bin/bash”-program stands for ?

Edit: Sorry, Option found, when typing in “man bash” :upside_down_face: :upside_down_face: :upside_down_face:

If I type man bash I get this:
-c If the -c option is present,
then commands are read from
the first non-option argu‐
ment command_string. If
there are arguments after
the command_string, the
first argument is assigned
to $0 and any remaining ar‐
guments are assigned to the
positional parameters. The
assignment to $0 sets the
name of the shell, which is
used in warning and error
messages.

1 Like