Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

318
Views
How can I use switch case statement in bash scripting to perform different operations?

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 tarts@10.171.0.10 <<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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!