Help with shell_command

Hi community,

I’m trying to run the following command as a shell_command

curl volumio.local/api/v1/commands/?cmd=stop ; PARAM=('pr' 'vo' 'tr' 'bs' 'ch' 'pr') ; VAL=('01' '13' '09' '12' '05' '00') ; for zone in 1 2 3 4 5 6; do for i in "${!PARAM[@]}"; do curl -XPOST -d ${VAL[i]} 1.2.3.4:8181/zones/1$zone/${PARAM[i]}; done ; done;

and got the following error in the logs

return code: 2:
b'/bin/sh: syntax error: unexpected "("\n'

It looks like it doesn’t like the array declaration. Is it actually using ash behind the scene ? (no array in ash)

The exact same command is running fine on my Mac.

Thanks

I just answered someone else who was trying to do advance shell scripting in YAML and I would suggest the same here - and that is to put that kind of a script into a script file and set your shell_command to run that file instead, this gives you so many extra benefits, the least of which is that you only restart HA one time to register the command and can modify that shell script file at will without restarting HA. Additionally you get a chance to make sure that the command works the way you want from a terminal session because not all unix’s are equal and some of the commands available in HA are less capable than their full unix counterparts.

1 Like

@CO_4X4 : Thanks. Very valid point I totally agree. I didn’t expect my command to be so lengthy when I started.

Just to answer my initial post, I ran a quick test that seems to confirm arrays are not usable through command_shell. The example below runs fine. It is actually a string with a space " " as the separator.

arraytest='string1 string2 string3' ; for s in $arraytest ; do echo "$s" ; done ;

If you are going to mix text qualifiers (single quotes and double quotes) you are going to fight with YAML since it’s going to encapsulate that into double quotes for the JSON conversion it saves. This is one of the reasons I prefer just scripting it in a file, I don’t have any of that YAML/JSON business to deal with and get pissed about :laughing:

Just don’t forget to chmod 0755 that script file so shell_commands can execute it :wink:

@CO_4X4 : Can’t agree more. Thank you for your insight, it was very helpful. Appreciate it.

So this is what I did and it is working as expected :slight_smile:

touch /config/shell/reset_params_house_audio.sh
chmod 0755 /config/shell/reset_params_house_audio.sh

reset_params_house_audio.sh >>

#!/bin/bash

curl $1/api/v1/commands/?cmd=stop

PARAM=('pr' 'vo' 'tr' 'bs' 'ch' 'pr')
VAL=('01' '13' '09' '12' '05' '00')

for zone in {1..6}
    do 
        for i in "${!PARAM[@]}"
            do
                curl -XPOST -d ${VAL[i]} $2/zones/1$zone/${PARAM[i]}
            done
    done

Call the shell_command:

reset_params_home_audio_cmd: bash /config/shell/reset_params_home_audio.sh volumio.local 1.2.3.4:8181

That is SO much simpler than hacking it together in a YAML file and having to constantly restart HA to test it. Also far more readable.

1 Like

Oh, and you can pass a single command line option in that shell too, FYI. Here’s what I to do send my TTS text to the computer that says whatever I put into HA:

speak: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] '~/Scripts/tts.sh "{{text}}"'

Then to speak via HA:

service: shell_command.speak
data:
  text: Hello world
1 Like