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 and shell scripting languages 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 10:04 PM
case statement bkan77 Shell Programming and Scripting 5 09-11-2007 06:54 PM
with Regard to Case Statement cosec Shell Programming and Scripting 4 09-04-2007 03:15 AM
Case Statement Zeta_Acosta Shell Programming and Scripting 19 04-06-2004 05:16 PM
case statement Bab00shka Shell Programming and Scripting 1 07-15-2002 06:31 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #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 12:08 PM.. Reason: Icon is wrong
  #2 (permalink)  
Old 05-10-2008
guruparan18 guruparan18 is offline
Registered User
  
 

Join Date: May 2008
Location: India
Posts: 37
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.
  #3 (permalink)  
Old 05-12-2008
fialia fialia is offline
Registered User
  
 

Join Date: May 2008
Location: Sweden
Posts: 2
Yes, that works. Thank you very much!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0