help with variable substitution in case statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with variable substitution in case statement
# 1  
Old 11-20-2008
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?
# 2  
Old 11-20-2008
Hi.

If you were using bash, you could use:

1) special expression notation feature
Code:
@($VALID_ENTRIES)) ... ;;

2) eval " ... entire case structure ..."
3) the literal, not the variable

The first does not work with ksh because it does not have the feature (nor built-in shopt, which is also required), so that leaves the second and third.

The second might require some re-writing, because you need to provide eval with a string of the entire case statement. That would normally be done with double quotes to allow the variable to be expanded. You are already using double quotes, so that would need to be fixed.

In this specific situation, I would use the literal.

There may be features in ksh which allows you to use the variable, but I didn't try anything other than the choices above. Perhaps a ksh expert will stop by with additional advice.

Best wishes ... cheers, drl
# 3  
Old 11-20-2008
Here I cannot use literal because the values are obtained at runtime and from that the user has to make a choice.
Also I did not understand how to use the second option.
# 4  
Old 11-20-2008
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
# 5  
Old 11-20-2008
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
# 6  
Old 11-22-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Case statement help

Hi I am new to shell scripting, I wanted to make a shell script that has a case statement asking the user to select their city 1)london 2)tokyo 3) etc., I then want the users input to be stored in a variable and echoed out in another script; so for example if the user selects tokyo, tokyo city code... (2 Replies)
Discussion started by: scriptnewbie
2 Replies

2. Shell Programming and Scripting

[Solved] Command Substitution and Variable Expansion within a Case

Hello All, I don't write scripts very often, and in this case I am stumped, although it may be a bug in the version of bash I have to use (it's not my system). I want to extract a specific string snippet from a block of text (coming from a log file) that is dependent on a bunch of other... (1 Reply)
Discussion started by: jaimielives
1 Replies

3. Shell Programming and Scripting

Case statement

Hello, The standard case statement :- case "$1" in "IE0263") commands;; "IE0264") commands;; esac is it possible to have :- case "$1" in "IE0263" OR "IE0878") commands;; "IE0264") commands;; esac Thanks (4 Replies)
Discussion started by: jmahal
4 Replies

4. Shell Programming and Scripting

case statement

Hi, I am writing case statement to execute some finction, my requirement is once one of the case statement is executed again it has to prompt for the option. for script in `echo "$Script_Selected"` do case $script in 1) getNoOFActUsers ;; 2) moveServerrOORotation ;; ... (2 Replies)
Discussion started by: Satyak
2 Replies

5. Shell Programming and Scripting

how to convert value in a variable from upper case to lower case

Hi, I have a variable $Ctrcd which contains country names in upper case and i want to convert them into lower case. I have tried so many solutions from already existing threads but couldn't get the correct one. Can anybody help me with this..... Thanks a lot.. (2 Replies)
Discussion started by: manmeet
2 Replies

6. UNIX for Dummies Questions & Answers

If or Case Statement

I want to write a program with the following variables: a=7000 b=24000 c=613.8 The user can enter two words: Vivid or Blue for example. The challenge is that the user might not want to write the words the way they appear. The user can write V or v or vivid or Vivid or write Blue or blue, or B,... (1 Reply)
Discussion started by: Ernst
1 Replies

7. Shell Programming and Scripting

Using variable in case statement

I want to do this: 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: Ex 2: case $answer in $cases) echo $answer;; x) break;; *) echo "Invalid... (2 Replies)
Discussion started by: fialia
2 Replies

8. Shell Programming and Scripting

case statement

hi all i'm writing a script and in it i need to prompt the user if the entered value is correct or not ,i wrote the following and its not working ,its executing the script even if i enter Y/N pls any help is appreciated echo "\nAre you sure you entered the right Destination Environment? y :... (5 Replies)
Discussion started by: bkan77
5 Replies

9. Shell Programming and Scripting

Case Statement

Can anyone please tell me why this wont work! Thanks so much! #!/bin/sh for file do case $file in *.*.*) echo Cannot have more than 1 dot exit ;; *'**'*) echo Cannot have more than 1 asterisk exit ;; *'*'*|?.) echo this is a target (19 Replies)
Discussion started by: Zeta_Acosta
19 Replies

10. Shell Programming and Scripting

case statement

Hi all, is it possible to create a 'dynamic' case statement. ie select option in `ls` do case satement depending on results of the above `ls` done I hope I have explained this ok! Thanks Helen (1 Reply)
Discussion started by: Bab00shka
1 Replies
Login or Register to Ask a Question