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?

Hi.

If you were using bash, you could use:

1) special expression notation feature

@($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

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.

Hi.

Like so:

#!/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:

% ./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:

@($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