LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Case statement with If statement (https://www.linuxquestions.org/questions/linux-newbie-8/case-statement-with-if-statement-597847/)

cbo0485 11-07-2007 02:01 PM

Case statement with If statement
 
Basically I am writing a script that will run other scripts.

I want to add an if statement, and I don't know what the best way about that would be. The main script has 5 cases, each case runs 3 different scripts and the last one is simply a quit. Inside each case though I want to add an IF statement that will verify that the user wants to run the scripts, and if the user chooses 'y' or 'Y' then it continues to run the scripts inside that case, and if the user chooses 'n' or 'N' then it quits the script or just returns to the main menu part.

Here is what I have so far, and I'm pretty new to scripting and I can't figure out the best way to incorporate the if statement.

select x in a1 a2 a3 a4 Quit
do
case $x in
a1)...path.../script1.sh
...path.../script2.sh
...path.../script3.sh
break;;
a2)...path.../script4.sh
...path.../script5.sh
...path.../script6.sh
break;;
a3)...path.../script7.sh
...path.../script8.sh
...path.../script9.sh
break;;
a4)...path.../script10.sh
...path.../script11.sh
...path.../script12.sh
break;;
*)break;;
esac
done


Thanks for your help.

Oh, and all the scripts being called inside the case statements, simply run a command.

bigrigdriver 11-07-2007 02:49 PM

I dunno. Sounds like a homework assignment to me.

So, I suggest you read the textbook for the course, the Absolute Bash-scripting Guide, and the Bash Guide for Beginners.

cbo0485 11-07-2007 03:03 PM

Not homework. I'm attempting to write a script that will shutdown/stop WebLogic instances. As it is right now we have many instances on many different servers and I am trying to make a weekly process go faster for me. The script will only allow you to choose which combo of instances you want to start up, and then it will auto run the scripts that start them.

a-b)
echo -en "You chose to start a and b, is this correct? "
response="n"
read response
if ["${response}" = y -a "${response}" = Y ]; then
/PATH/startservera.sh
/PATH/startserverb.sh
/PATH/startservere.sh
fi
That is what I currently have in my first case that I want to have in each one. I obviously changed names and stuff though. Basically I have four cases that can start any combo of server abcd where only A or B can be started at one time, and only C or D can be started, and server e starts regardless of which option is chosen.

EDIT:
This is the error I keep getting
./test2.sh: line 11: [y: command not found

cbo0485 11-07-2007 03:24 PM

Well, I got it through much searching and tweaking and syntax tweaking.

Final IF solution was this:

if [[ "$response" = 'y' || "$response" = 'Y' ]]

I had tried that with only one set of brackets and it didn't work, No idea why a second set made it work.

homey 11-07-2007 08:05 PM

You could chop it down abit for the the upper/lower yes part.
For example:
Code:

read -p "You chose to start a and b, is this correct? " response

if [[ "$response" = [yY]* ]]; then
  echo "do this"
else
  echo "do something else"
fi



All times are GMT -5. The time now is 11:17 AM.