|
|
|
|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | 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 and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
help with variable substitution in case statement
I have a script where I take input from user and see if it falls in list of valid entry.
--------------- #!/bin/ksh VALID_ENTRIES="4|5|6" echo "enter your choice" read reply IFS="|" echo "valid entries are $VALID_ENTRIES" case "$REPLY" in Q|q ) IFS="$IFS_SAVE";return ;; $VALID_ENTRIES ) IFS="$IFS_SAVE" echo "The entry is valid";; *) echo "invalid entry" esac ------------------------------------- Now even if user enters a value from 4,5,6 I always get output as invalid entry. The value of VALID_ENTRIES doesn't seem to get substituted properly. However if in case statement instead of $VALID_ENTRIES I directly write 4|5|6 for testing purpose it works. So this shows that exact substitution is not made. Can somebody help me with this? |
| Sponsored Links |
|
|
|
||||
|
Hi. Like so: Code:
#!/bin/bash - # @(#) s2 Demonstrate variable in case, eval, extglob (bash). echo echo "(Versions displayed with local utility \"version\")" version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) set -o nounset echo echo " Results:" x="a|b|c|d" echo " x is :$x:" echo " Desiring yes, but getting no:" case a in $x) echo yes;; *) echo no;; esac echo echo " Expecting yes:" case a in a|b|c|d) echo yes;; *) echo no;; esac echo echo " Various features, expecting yes (echo included):" shopt -s extglob case a in @($(echo $x))) echo yes;; *) echo no;; esac echo echo " Various features, expecting yes (echo omitted):" shopt -s extglob case a in @($x)) echo yes;; *) echo no;; esac echo echo " With eval:" eval "case a in $x) echo yes;; *) echo no;; esac " exit 0 Producing: Code:
% ./s2 (Versions displayed with local utility "version") Linux 2.6.11-x1 GNU bash 2.05b.0 Results: x is :a|b|c|d: Desiring yes, but getting no: no Expecting yes: yes Various features, expecting yes (echo included): yes Various features, expecting yes (echo omitted): yes With eval: yes The last illustration uses eval ... cheers, drl |
|
||||
|
Hi. I just ran the script s2 (above) with ksh 93s+, and the case selector construction: Code:
@($variable)) ... ;; worked correctly. So if you have that version, you should be OK. It does not work with pdksh 5.2.14 99/07/13.2 See man ksh for details (section File Name Generation.) ... cheers, drl |
|
||||
|
Hi.
With ksh 93+, a solution with IFS="|" also works. Note that in the original, IFS="$IFS_SAVE" seems to be done without the IFS_SAVE variable being first set... cheers, drl |
| Sponsored Links |
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to convert value in a variable from upper case to lower case | manmeet | Shell Programming and Scripting | 2 | 11-13-2008 02:22 PM |
| If or Case Statement | Ernst | UNIX for Dummies Questions & Answers | 1 | 09-24-2008 06:00 PM |
| Using variable in case statement | fialia | Shell Programming and Scripting | 2 | 05-12-2008 05:54 AM |
| case statement | bkan77 | Shell Programming and Scripting | 5 | 09-11-2007 06:54 PM |
| Case Statement | Zeta_Acosta | Shell Programming and Scripting | 19 | 04-06-2004 05:16 PM |