The UNIX Forums  



Go Back   The UNIX Forums > Top Forums > Shell Programming and Scripting
Home Forums Register Rules & FAQDonate Members List Search Today's Posts Mark Forums Read

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

Reply
 
Submit Tools Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 1 Week Ago
Registered User
 
Join Date: May 2008
Location: Sweden
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 : 1 Week Ago at 10:08 AM. Reason: Icon is wrong
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 6 Days Ago
Registered User
 
Join Date: May 2008
Location: India
Posts: 11
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Forum Sponsor
  #3 (permalink)  
Old 4 Days Ago
Registered User
 
Join Date: May 2008
Location: Sweden
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Yes, that works. Thank you very much!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
help needed in using case statement jisha Shell Programming and Scripting 0 01-16-2008 02:33 AM
case statement bkan77 Shell Programming and Scripting 5 09-11-2007 04:54 PM
with Regard to Case Statement cosec Shell Programming and Scripting 4 09-04-2007 01:15 AM
Case Statement Zeta_Acosta Shell Programming and Scripting 19 04-06-2004 03:16 PM
case statement Bab00shka Shell Programming and Scripting 1 07-15-2002 04:31 AM


web tracker

All times are GMT -5. The time now is 12:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
UNIX Forum Content Copyright ©1993-2008 SilkRoad Asia All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93