Bash Script Return Value

Hi.
I have a bash script which pings devices in my home and returns the number of devices which are offline. I tired to return this value with « echo » as well as with « exit » but I always get 0 as return. When I hit the test button in Jeedom of the script it says: « Command result:0 » and not the e.g. expected value of 2. How can I return a value from a bash script with the Script Plugin?
Thank you very much. S.

Hi,

Return is send to Jeedom with echo command.
So if your echo return 0 and not 2 it’s probably because you have a issue somewhere in your code.

Without sharing your code and a screenshot of your plugin conf we can’t help you

Hi,

Also, don’t forget that just hitting the « test » button doesn’t refresh the value but returns the current one.
To refresh the command, so to run your bash script again, better save the script (jeedom device) to be sure it is up to date during your test / initial setup each time you change the bash script.

Once the script is ok then you can setup the cron to refresh commands automatically.

Take a look also at the #plugin-networks which allow to do what you are trying to achieve pretty easily.

Thank you. This helped. I had several echos throughout the script. I commented them and left ONLY ONE echo at the very end. This fixed the issue. Now it is working as expected. Thanks for your help.

Just in case this is usefull for someone below my bash script. I have a jeedom scene which sends me an email if there are more than 3 devices offline. This helps me to detect a power cut since me jeedom rasp and internet rounter are on a small UPS but the rest of the wifi devices is not - which means if several are down at the same time this might mean the main power might be down.

#!/bin/bash
declare -A devices=(
["Shelly 1"]="192.168.0.20"
["Shelly 2"]="192.168.0.21"
["Shelly 3"]="192.168.0.22"
)

deaddevices=0

for device in "${!devices[@]}";
do
        ip="${devices[$device]}"
        if ! ping -c 1 -W 1 "$ip" 2>&1 >/dev/null; then
                        #echo "$device with $ip does not respond"
            deaddevices=$((deaddevices+1))
        fi
done
echo $deaddevices;

Thank you Mips.

To save the script and command after each modification definitely helped with debugging. Thank you for the tip.

I know the network plugin and use it already for presence detection (scan for mobile phone MAC). Its great and allows to ping etc.

I have a rather large list of IPs and prefer to use the script to run over the list. This also helps to count and tricker a « summary » email alert without complicating my scene a lot.

Thank you!
S.

1 « J'aime »