Home > Net >  How can I use switch case statement in bash scripting to perform different operations?
How can I use switch case statement in bash scripting to perform different operations?

Time:11-03

I have this script that is currently launching a tool's graphical user interface. I want to add another functionality in switch case which is to kill the tool if needed. Right now I have different scripts doing this. I want to integrate the bits and pieces.

Currently, the script I have looks like this:

#!/bin/bash

tart_array=($1)

ip=()
tarts=()

for i in ${tart_array[@]}; do
        echo "Tart #: $i"
        case "$i" in

                1)
                IP=10.171.0.10
                ;;
                2)
                IP=10.171.0.11
                ;;
                3)
                IP=10.171.0.12
                ;;
                4)
                IP=10.171.0.13
                ;;
                5)
                IP=10.171.0.14
                ;;
                *)
                echo "Invalid TARTS '$i'" >&2;
                exit
                ;;
        esac
        ip =(${IP=[i]})
#       echo "$ip"
        tarts=(${tart_array[@]})
#       echo "$tarts"
        echo "    ----  Launching Tart $i  ----  "
                sshpass -p "tart123" ssh -Y -X -L 5900:$IP:5901 tarts@$IP <<EOF1
                  export DISPLAY=:1
                gnome-terminal -e "bash -c \"pwd; cd /home/tarts; pwd; ./launch_tarts.sh exec bash\""
                exit
EOF1
done

Just like Launching TARTS I want to add Killing TARTS in the above switch case statement. Just for example for killing the tool I am doing:

 echo "Killing Tart $i"
                        sshpass -p "tart123"  ssh -tt -o StrictHostKeyChecking=no [email protected] <<EOF
                        . ./tartsenvironfile.8.1.1.0
                        nohup yes | kill_tarts mcgdrv &
                        nohup yes | kill_tarts server &
                        pkill -f traf
                        pkill -f terminal-server
                        exit
EOF

Can some one please guide how can I integrate Launching and Killing functionality in my script using the same switch case?

CodePudding user response:

You can use functions. You can put the ip addresses in an array, using the corresponding tart number as the index. You can also leverage character sets if you need to validate 1-5.

Here’s a rough outline for what I think you’re getting at. Use like: tart-tool kill 1 3 2. I don’t fully understand your goal, but maybe it will give you some ideas.

#!/bin/bash

launch_tart () {
    local tart=$1
    local ip=${ip[tart]}

    echo "launching tart $tart…"
    sshpass tarts@$ip # etc
}

kill_tart () {
    local tart=$1
    local ip=${ip[tart]}

    echo "killing tart $tart…"
    sshpass tarts@$ip # etc
}

ip[1]=10.171.0.10
ip[2]=10.171.0.11
# etc

case $1 in
    kill) function=kill_tart;;
    launch) function=launch_tart;;
    *) exit 1
esac

shift

for tart in "$@"; do
    [[ "$tart" == [1-5] ]] || { echo error >&2; exit 1; }

    $function $tart
done
  • Related