The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sh -help with case statement (should be simple) kuliksco Shell Programming and Scripting 1 11-19-2007 06:04 PM
case statement bkan77 Shell Programming and Scripting 5 09-11-2007 02:54 PM
with Regard to Case Statement cosec Shell Programming and Scripting 4 09-03-2007 11:15 PM
Case Statement Zeta_Acosta Shell Programming and Scripting 19 04-06-2004 01:16 PM
case statement Bab00shka Shell Programming and Scripting 1 07-15-2002 02:31 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-09-2008
Registered User
 

Join Date: May 2008
Location: Sweden
Posts: 2
Question Using variable in case statement

I want to do this:
Code:
Ex 1:
case $answer in
  1|2|3|4|5) echo $answer;;
  x) break;;
  *) echo "Invalid selection. Try again.";;
esac
But I need the part "1|2|3|4|5" to be fetched from a variable, like so:
Code:
Ex 2:
case $answer in
  $cases) echo $answer;;
  x) break;;
  *) echo "Invalid selection. Try again.";;
esac
So if the input is 1-n, I want my program to execute (its not really echo I plan on executing, but I will send $answer as the action's input).

Is that possible? What I really want to know is, can the number of cases be variable depending on when the script is run? If so, how do I achieve it?

When written like in ex 2 my input is interpreted as * every time. I have also tried replacing "$cases" with "´echo $cases´" with the same result.

The script is written in bash and runs on Sun Solaris 9.

Last edited by fialia; 05-09-2008 at 08:08 AM. Reason: Icon is wrong
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-10-2008
Registered User
 

Join Date: May 2008
Location: India
Posts: 25
Try this

Quote:
bash-2.03$ b=[1,2,3,4]
bash-2.03$ case $ans in
> $b) echo $ans ;;
> x) break ;;
> *) echo "Error"
> esac
It should work.
Reply With Quote
  #3 (permalink)  
Old 05-12-2008
Registered User
 

Join Date: May 2008
Location: Sweden
Posts: 2
Yes, that works. Thank you very much!
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0