Multiple conditions in a CASE statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple conditions in a CASE statement
# 1  
Old 03-13-2011
Multiple conditions in a CASE statement

is it possible to use multiple conditions in a CASE statement? And if so, what is the syntax? I'm trying to use one but can't seem to get it right. I want the statement to be

Code:
CASE $vendor OR $alias
    condition 1)  statements;
    condition 2) statements;
    etc.
esac

but I keep getting syntax errors, no matter what I try. Can somene tell me what the right syntax is
# 2  
Old 03-13-2011
How about this?

Code:
CASE $vendor  in
    condition 1|condition 3)  statements;;
    condition 2|condition 4) statements;;
    etc.
esac

# 3  
Old 03-13-2011
Combine the two vars with a seperator eg for green yellow red conditions:

Code:
CASE ${vendor}+${alias} in
    *+green|green+*) statements;;
    *+yellow|yellow+*)  statements;;
    *+red|red+*)  statements;;
  etc.
esac


Last edited by Chubler_XL; 03-13-2011 at 10:13 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 03-13-2011
Chubler --

I'm not sure how to interpret that statement. What I want to do is have the CASE statement test the value of the variables. So, for example I need to have the statements executed if either vendor or alias is the value in the subsequent statements. Is that what your answer means? My script, at this point, is allowing the user to enter values for two variables (as you can see), but needs to test if either of them are p, m or e, in which case the script goes back to a previous menu or exits the script altogether.
# 5  
Old 03-13-2011
Yes that is what the example I posted is doing.

To explain the values of the two variables are joined together with a + in between. (For example green+black )

The case statement *+green|green+*) will match green+black (but not yellow+black)

So for your example just replace green with m ; yellow with p and red with e:

Code:
case ${vendor}+${alias} in
    *+m|m+*) // go back to main;;
    *+p|p+*) // go to previous ;;
    *+e|e+*) // exit menu;;
 etc.
esae

# 6  
Old 03-14-2011
Thanks -- so let me see if I understand, just to be clear. The help command doesn't say anything about the + sign, nor does the bash book I have.

Just to be clear, the statement means 'Check the value of both vendor and alias. [If vendor is not one of the test criteria and is followed or preceded by the test conditions ] OR [Alias is not one of the test criteria and is followed by any of the test criteria], then execute the corresponding commands. Is that correct ?

One more thing -- how would it be written if I wanted it to check for upper and lower case, which Is what I want to do.

Last edited by Straitsfan; 03-14-2011 at 05:22 PM..
# 7  
Old 03-14-2011
The + sign used here isn't anything special to the shell it's just a char we have thrown inbetween the two variables. One could just as well use "_" or "," as long as the character isn't in the strings being tested.

What is happening is the two strings are joined together and then the glob pattern matching capabilites of the case statement are used to match what is required.

So for upper or lower case and using "_" we would have:

Code:
case ${vendor}_${alias} in
    *_[Mm]|[Mm]_*) // go back to main;;
    *_[Pp]|[Pp]_*) // go to previous ;;
    *_[Ee]|[Ee]_*) // exit menu;;
etc.
esae

The expression "*_[Mm]|[Mm]_*" equates to:
Code:
    <zero or more chars>_<upper or lower case M> ..or..
    <upper or lower case M>_<zero or more chars>

Some example strings that will match this are:
Code:
    M_NotImportant
    m_
    m_back to main
    back to main_M


Last edited by Chubler_XL; 03-15-2011 at 01:26 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Check Multiple conditions in IF statement?

I wish to check two conditions inside the if statement Condition 1: The two file contents should be identical // using cmp command for this. Condition 2: The two filenames should NOT be the same. This is what i did in vain. if ]; then where entry1 and entry2 are ls *.txt | while... (7 Replies)
Discussion started by: mohtashims
7 Replies

2. Shell Programming and Scripting

Multiple conditions inside if statement

I was trying to write multiple conditions inside the if statement but its not working. export VAR_NM=abc.txt export CURR_DT=20131011 export PREV_DT=20131012 if && then echo "Yes" else echo "NO" fi It should return Yes but returning NO always.Appreciate any help. (3 Replies)
Discussion started by: dr46014
3 Replies

3. Shell Programming and Scripting

Multiple conditions inside my if statement

Hello, I am using shell scripting and I am recieving odd results from my if statement if I want it to enter the loop only if L1 is equal to zero and one of the other criteria are filled, however it is entering at other times as well. What can i do to fix this? i tried seperating it... (6 Replies)
Discussion started by: ryddner
6 Replies

4. Shell Programming and Scripting

Choose multiple conditions in case?

Hi all, I am attempting to create a shell script to optimize some routine process. I want this script can let user select the actions they want to do. But I met a problem that my script can only read one input and then do one action. Is it possible to let my script to run more than one... (2 Replies)
Discussion started by: kaiya
2 Replies

5. UNIX for Dummies Questions & Answers

Multiple Find Conditions in IF Statement - UNIX

When I try the below if Condition with single condition its working fine. But when I try to Club both its working . But giving wrong results. In my case cond1 = -f ${filename1} = true cond2 = -f ${filename2} = true But Cond1 & Cond2 is resulting in False ??? Please advise ... (5 Replies)
Discussion started by: Shiny_Reddy
5 Replies

6. Shell Programming and Scripting

Multiple Conditions Perl if Statement

Hello, I'm trying to put together a script that involves pulling data from a config file. I'm attempting to write an if statement to validate one of the pieces of data from the config file, but I think I'm fat fingering it somehow. $config{VALUE} is being pulled from a config file but can only... (4 Replies)
Discussion started by: Picch
4 Replies

7. Shell Programming and Scripting

multiple lines inside a case statement

echo "please enter ur choice.. 1. Make a file. 2. Display contents 3. Copy the file 4. Rename the file 5. Delete the file 6. Exit" read choice case $choice in 1 ) echo enter the file name read fname if then echo... (2 Replies)
Discussion started by: gotam
2 Replies

8. Shell Programming and Scripting

If statement with multiple conditions

I have a script that runs on multiple servers. What I want to do is have the script do the following: if $(hostname) is equal to server or server2 then TO_DIR=go else TO_DIR=stop fi I have tried: if if ] Server is hpux. any ideas? (1 Reply)
Discussion started by: cpolikowsky
1 Replies

9. Shell Programming and Scripting

if statement with two conditions

Hi, I am trying to use && set up to match two conditions within ksh: if && then '''Do something if somehow, I keep getting error message telling me that ] is missing. What's wrong with my code? Thanks a lot for your help! (1 Reply)
Discussion started by: cin2000
1 Replies
Login or Register to Ask a Question