Shell scripting with case statement

Foe example we have three environments int,qa and prod.Each environment has some number of servers.

int=Server1,Server2,Server3
qa=Server4,Server5,Server6
prod=Server7,Server8,Server9

echo "Enter the Environment i.e int,qa,prod"
read env
case $env in 

int)
## Need command where all the servers in int should loop one by one and should assign the Server1 to adminHost###
###If i select int.Server1 should assign to adminHost i.e $adminHost should be in Server1 and then Server2 so on###
;;
qa)
#### Same as INT####
;;
prod)
### Same as int####
;;

After this i have my own script which should execute.

Can Some one please help how to write a case statement in shell script.

echo "Enter the Environment i.e int,qa,prod"
read env
case $env in 
     int)
          Servers="Server1 Server2 Server3"
          ;;

     qa)
          Servers="Server4 Server5 Server6"
          ;;

     prod)
          Servers="Server7 Server8 Server9"
          ;;

esac

for adminHost in $Servers ; do
     # whatever you want to do goes here
done

I hope this helps.

bakunin

1 Like

Thanks for you reply.

That worked.

What if i have servers listed in a file called intservers.txt ,qaservers.txt,prodservers.txt and these servers list under some path say /opt/app/serverslist/ intservers.txt

How would those files be formatted? One server per line, a comma separated list in one line, ...?

One server per line.
Either way is also ok for me.

Assuming you're using bash (which you failed to mention), try

declare -A SRVFILES=([int]=intservers.txt [qa]=qaservers.txt [prod]=prodservers.txt)
read -p"Enter env: " ENV
Enter env: int
while read SRV; do echo $SRV; done < /opt/app/serverslist/${SRVFILES[$ENV]}
Server1
Server2
Server3
1 Like

Really appreciate to all of your efforts.Thanks a million in advance.
I am new to scripting,i will try to explain what i am looking for again.Below is my script which is working fine.As i said before if i have to pull the servers from txt file and it should assign each server to adminHost, so that the script will execute on all the remote servers.I dont want to mention Server1 , Server2 ..so on under Servers="Server1 Server2 Server3" .It should bring the serverslist from a file and assign one server to adminHost.

#!/bin/bash
# ------------------
# WeblogicPatch.sh
# ------------------
###########################################################################################################################
appUsr="admin"
PATCH_HOME="/opt/oracle/weblogic/12.2/OPatch"
BN_HOME="/opt/was/naresh/app/Binaries/12.2/Patch"
BINARIES_HOME="/opt/app/Binaries"
SVRSCRIPT="/opt/oracle/weblogic/svrscript"
stop_script_name="startup_WL.sh"
SERVERS_RUNNING="/home/wladmin"
SERVERS="/opt/was/naresh"
SRVFILES="/opt/was/naresh/SRVFILES" ##{ list of files with servers in it are intserver.txt,qa...prod}##

echo "Enter the Environment i.e int,qa,prod"
read env
case $env in
     int)
          Servers="Server1 Server2 Server3"
          ;;

     qa)
          Servers="Server4 Server5 Server6"
          ;;

     prod)
          Servers="Server7 Server8 Server9"
          ;;

esac

for adminHost in $Servers ; do
     # whatever you want to do goes here

echo "##################"
echo "Copying the Patch Files"
echo "###################"

scp -p ${BN_HOME}/*.zip  ${appUsr}@${adminHost}:${BINARIES_HOME}

echo "##########################"
echo " Stopping All the Server "
echo "###########################"
ssh ${appUsr}@${adminHost} cd $svrscript; ./startup_WL.sh stop;

sleep 10

echo  "#################################################"
echo  "Unzip the Patch  Files and Move to the Patch Home"
echo  "#################################################"
ssh ${appUsr}@${adminHost} cd ${BINARIES_HOME}; find . -type f -mmin -10 -name '*.zip' -exec sh -c 'unzip -d "`dirname \"{}\"`" "{}"' ';'; 
sleep 10;

echo  "###############"
echo  "Applying Patch"
echo  "################"
ssh ${appUsr}@${adminHost} cd ${PATCH_HOME}; ls -ltr; var1=$(ls | grep -E '^[0-9]+$'| xargs| sed 's/ /,/g');

sleep 10;

echo  "################################################################################"
echo  "Starting All Servers"
echo  "################################################################################"
ssh ${appUsr}@${adminHost} cd $svrscript; ./startup_WL.sh start;

sleep 10;

echo "Servers are Up and Running "

echo  "################################################################################"
echo  "List the Servers That are Running"
echo  "################################################################################"
ssh ${appUsr}@${adminHost} cd $SERVERS_RUNNING;./listWeblogicServers.sh ;

sleep 10;
done
exit

Thanks

And what stops you from applying what you learned in post#6?

How can i substitute those written server names to adminHost ??

while read adminHost; do echo $adminHost; done < /opt/app/serverslist/${SRVFILES[$ENV]}