1

I have a .conf file that I am calling in my main script, which contains a case statement. In this file, I have a series of ssh commands that I am combining into a single variable/array/function (I've tried multiple methods) to be executed when the condition is met in my case statement. For context, this is a script to auto-shutdown clients.

~/variables.conf

#!/bin/sh
CLIENT_1="ssh admin@1.1.1.1 shutdown -r now"
CLIENT_2="ssh admin@1.1.1.2 shutdown -r now"
CLIENT_ALL() { $CLIENT_1 ; $CLIENT_2 ; }
#also tried with similar results
#CLIENT_ALL="$CLIENT_1; $CLIENT_2"
#CLIENT_ALL=($CLIENT_1 $CLIENT_2)

To make sure this portion of code is working and the variables are passing, I run a test.sh and execute from CLI.

~/variables.test.sh

#!/bin/sh
. ~/variables.conf
CLIENT_ALL

Great, everything works. My two clients restart successfully - ssh keys stored so no prompt to enter password.

But when this is called from my case statement, things go wrong:

~/script.sh

#!/bin/sh
. ~/variables.conf

case $1 in
          trigger1)
                    logger <message> #this is working fine
                    printf <message> | msmtp <email> #this is working fine
                    CLIENT_ALL
                    ;;
          *)
                    logger "Unrecognized command: $1"
                    ;;
esac
                    

What happens when this triggers: it logs, it sends an email but only the first client gets the ssh command to reboot. It passes the first variable $CLIENT_1 and then stops. I've tried a variety of ways to define and package the ssh commands, as well as a variety of ways to call them in the case statement, but always with the same results. I am certain that there is something about case statement rules/logic that I am overlooking that will explain this behavior and a correct way to make this work.

For my use-case, I need to use a case statement. My goal is to have a single command in the case statement so that the main script doesn't have to be modified - only the .conf needs to be updated if clients are added/removed.

Any help would be greatly appreciated.

BAMzilla
  • 11
  • 1
  • 3
    See [How can I debug a Bash script?](https://stackoverflow.com/q/951336/4154375) and [How to debug a bash script?](https://unix.stackexchange.com/q/155551/264812). Note that the question is tagged 'bash' but the code uses '/bin/sh'. See [Difference between sh and Bash](https://stackoverflow.com/q/5725296/4154375). Using the 'shell' tag might attract more interest. – pjh May 11 '22 at 09:26
  • 3
    Being in a `case` statement shouldn't affect this at all, and it works when I test it (well, except those IPs are DNS servers, not ssh). I don't know what might be causing it to fail for you, but on general principles I'd recommend not storing your commands in variables (i.e. `CLIENT_1` and `CLIENT_2). Variables are for storing data, functions are for storing executable code. You can get away with putting simple commands in variables, but there are a bunch of parsing oddities that cause trouble for anything at all complex, and it's best to just avoid it altogether. – Gordon Davisson May 11 '22 at 09:31
  • 1
    See [How to store a command in a variable in Linux?](https://stackoverflow.com/q/5615717/4154375) and [BashFAQ/050 (I'm trying to put a command in a variable, but the complex cases always fail!)](https://mywiki.wooledge.org/BashFAQ/050). – pjh May 11 '22 at 10:22

0 Answers0