Shell scripting with case statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell scripting with case statement
# 1  
Old 09-13-2017
Shell scripting with case statement

Foe example we have three environments int,qa and prod.Each environment has some number of servers.
Code:
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.

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-tags for code, terminal output and file content. Thank you.

Last edited by bakunin; 09-13-2017 at 10:14 AM..
# 2  
Old 09-13-2017
Code:
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
This User Gave Thanks to bakunin For This Post:
# 3  
Old 09-13-2017
Shell scripting with case statement

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
# 4  
Old 09-13-2017
How would those files be formatted? One server per line, a comma separated list in one line, ...?
# 5  
Old 09-13-2017
Shell scripting with case statement

One server per line.
Either way is also ok for me.
# 6  
Old 09-13-2017
Assuming you're using bash (which you failed to mention), try
Code:
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

This User Gave Thanks to RudiC For This Post:
# 7  
Old 09-13-2017
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.


Code:
#!/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


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 09-13-2017 at 01:08 PM.. Reason: Added CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Linux Shell Scripting If-else and Case

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: This is what is asked: If the user enters ‘3’, prompt the user for two file names. Verify that the file names... (2 Replies)
Discussion started by: cindy01
2 Replies

2. Shell Programming and Scripting

Using shell to generate case statement

Hi Gurus, I have a very weird requirement and have no clue to resolve the issue. please help me get out this difficulty below two tables, table1 contains the column name. D means this column used for the rule. for example: rule 0 is all columns have value, rule1 is col3 and col7 have no value.... (2 Replies)
Discussion started by: Torhong
2 Replies

3. Shell Programming and Scripting

Case statement in UNIX shell script

have written the below code to check whether the string received from user is a file name or dir using case statement, but its going into default case*). #!/bin/sh #Get a string from user and check whether its a existing filename or not rm str2 rm str3 echo "enter a file \c" read fil... (8 Replies)
Discussion started by: Mohan0509
8 Replies

4. Shell Programming and Scripting

Menu and case statement scripting

hi all i am trying to get help with writing a script using case statement to display menu as 1) Authentication log 2) System log 3) Messages 4) Dmesg 5) Boot log Q) Exit When selecting the menu by 1 or 2 or 3 o 4 or 5, it should display the last 10 lines of the log files, if... (3 Replies)
Discussion started by: renegade11
3 Replies

5. Shell Programming and Scripting

Pass values to case statement in a function korn shell

I'm in the process of writng a function that consists of a case statement is there a way of calling the function and passing a value to it? ie function1 () { case opt1 do ..... opt2 do..... esac } function opt1 I'm aware the syntax is not correct, but you get the general idea. (1 Reply)
Discussion started by: squrcles
1 Replies

6. Shell Programming and Scripting

Shell case statement

echo -e "Select: \c" read IN pattern="1-20" case $IN in ) echo "Selected: $IN" ;; *) echo "Invalid selection: $IN" ;; esac # sh test Select: 10 Invalid selection: 10 # sh test Select: 2 (6 Replies)
Discussion started by: Ikon
6 Replies

7. UNIX for Dummies Questions & Answers

case statement in UNIX scripting (ksh)

Hi, I have a script like below : #!/bin/ksh echo "Do you want to export all docs ?" read alld echo "Do you want to export template or report only " read temr case && ] #arguments ;; case && ] #arguments ;; case && ] #arguments ;; (4 Replies)
Discussion started by: luna_soleil
4 Replies

8. Shell Programming and Scripting

shell script case statement

In a case statement like below : case $rental in "car") echo "For $rental Rs.20 per k/m";; "van") echo "For $rental Rs.10 per k/m";; "jeep") echo "For $rental Rs.5 per k/m";; "bicycle") echo "For $rental 20 paisa per k/m";; *) echo "Sorry, I can not gat a $rental for you";;... (4 Replies)
Discussion started by: sriram003
4 Replies

9. Shell Programming and Scripting

what is problem with this small shell script.. case statement related

Hi All, this small script is written to recognize user input character.. it is in small case .. upeer case or is a number... but when i input first capital letter say A.. it always gives small character.... what is the problem. #!/bin/bash echo "Enter the character" read a case $a in )... (2 Replies)
Discussion started by: johnray31
2 Replies

10. Shell Programming and Scripting

Shell script automation using case statement

Hi, I'm trying to write a shell script that has a menu and then dependant on the selection, will automate some samba file transfer. The problem is when I run the code without the case statement it runs fine. but when I put the case statement in the only way I can get the code to run is to... (6 Replies)
Discussion started by: ianf
6 Replies
Login or Register to Ask a Question