View Single Post
  #1 (permalink)  
Old 05-09-2008
fialia fialia is offline
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