[SOLVED] ExecuteCommandline shell script

Hi community,

I’m using openhab for more than one year now and it works really stable and reliable.

Now I want to integrate a buetooth box and the first function I programmed is to check if the bluetooth connection from my raspberry pi (accessable via ssh) is working.

The shell script is quite simple and looks like this:

btinfo=$(ssh pi@raspberry.dahoim 'echo -e "info 00:13:7B:69:3C:94" | bluetoothctl')

if [[ $btinfo == *"Connected: no"* ]] ; then
  echo "no"
elif [[ $btinfo == *"Connected: yes"* ]] ; then
   echo "yes"
fi

The rule to trigger it looks like this:

rule "check-BT-connection-denon"
when
  Item AU_D01_01 received command ON
then
  logInfo("BTcheck","Rule to check BT connection triggered")
  var String btinfo
  btinfo = executeCommandLine("/etc/openhab2/myscripts/btcheck.sh",100000)
  logInfo("BTcheck",btinfo)
end

Unfortunately I can see from the logs that there is an error saying:

2019-06-03 18:02:47.951 [INFO ] [lipse.smarthome.model.script.BTcheck] - /etc/openhab2/myscripts/btcheck.sh: 3: /etc/openhab2/myscripts/btcheck.sh: [[: not found

The shell script is working fine if I just run it via bash shell (also with the openhab user)
When I remove the if else statement from the shell script and just echo the variable $btinfo it gives the output of the command so this looks not like an issue with wrong permissions
Are there some limitations for executeCommandLine which I overlook in the documentation?

I’ve some other shell scripts running but no other script have an if statement.

Hope you can help me to get the script running. If this would not work, may I can try to run an python script doing the same thing or give the exec binding a try but I want to avoid that if possible.

Openhab runs on version 2.4.0. on a Debian Server 9 VM

Please How to use code fences.

You appear to be missing the #!/bin/bash as the first line of your shell script. Not sure that is relevant but it could be important because I don’t have any idea what shell would be used to execute this script when it’s run from openHAB. There might not even be a shell. The error seems to point to there being some shell but I’ll bet it is sh or something else that uses a different syntax and doesn’t understand the [[.

By making the above the first line of the script, you force the script to be executed using bash.

1 Like

Oh dear, so easy :grimacing:! Thanks a lot - also for the fast response. This solved my problem.
I’ve updated my initial post with the code fences as well.