Operating System - HP-UX
1847345 Members
2520 Online
110264 Solutions
New Discussion

Case-statement and variables

 
SOLVED
Go to solution
Wessel Baptist
Advisor

Case-statement and variables

Why does this script respond whith "Wrong..." and "Right..." which I expected?

export POSSIBLE="6.6.01|6.6.02"
export CURRENT="6.6.01"
case $CURRENT in
$POSSIBLE)
echo "Right $CURRENT"
;;
(*)
echo "Wrong $CURRENT"
esac
logics take you from A to B, imagination takes you anywhere
3 REPLIES 3
Bill McNAMARA_1
Honored Contributor
Solution

Re: Case-statement and variables

why don't you switch around the 6.6.01 in the
first export which may explain it.
Who cares anyway... it works!

Bill
It works for me (tm)
Wessel Baptist
Advisor

Re: Case-statement and variables

Sorry. I typed too fast, I forgot the word "NOT".
I mean: Why does this script respond whith "Wrong..." and NOT whith "Right..." which I expected?

logics take you from A to B, imagination takes you anywhere
John Palmer
Honored Contributor

Re: Case-statement and variables

Hi,

I think that it fails because of the order in which the shell processes the case statement.
The first thing that it does is to split the lines into tokens that are serarated by the standard set of metacharacters:-
space tab newline ; ( ) < > | &

Later it performs parameter substitution on $POSSIBLE but this is too late for it to correctly handle the | characters.

You can force the shell to re-evaluate the command with the 'eval' statement and indeed if you try:-

eval "

"

then you get the result that you expected. Quoting of this form can cause problems though.

Hope this helps,

Regards,
John