Operating System - HP-UX
1834372 Members
2161 Online
110066 Solutions
New Discussion

Re: using variables in case command

 
gary phipps
Frequent Advisor

using variables in case command

I've written a script similar to the following...

case $var1 in
$var2 )
do stuff ;;
* )
do nothing ;;
esac

...where $var2 = "001|002|003" but $var2 is not being translated correctly, or at least how I thought it would be.
If I set $var2 to be a single value (without the pipes) it works fine.

Can anyone advise on the correct syntax?
10 REPLIES 10
hpuxrox
Respected Contributor

Re: using variables in case command

Try putting 001|002|003 in single ' quotes.
$var2 = '001|002|003'
Mark Greene_1
Honored Contributor

Re: using variables in case command

the pipe is just another character to the shell, not a delimeter. You can use tabs, or change the FS (field separator) variable to the pipe:

OLD_FS=$FS
FS="|"
CASE
...
ESAC
FS=$OLD_FS

HTH
mark
the future will be a lot like now, only later
S.K. Chan
Honored Contributor

Re: using variables in case command

The "|" is being translated as logical or in the case statement. Try this instead ..

case $var1 in
"$var2" )
do stuff ;;
* )
do nothing ;;
esac

S.K. Chan
Honored Contributor

Re: using variables in case command

I totally misunderstood your q, sorry .. why not use pattern matching in the case statement ..?

case $var1 in
00[1-3] )
do stuff ;;
* )
do nothing ;;
esac

SHABU KHAN
Trusted Contributor

Re: using variables in case command


Gary,

Try escaping the pipe with backslash like this:

...where $var2 = "001\|002\|003"

and then: double quotes var2

case $var1 in
"${var2}" )
do stuff ;;
* )
do nothing ;;
esac

-Shabu
Wodisch
Honored Contributor

Re: using variables in case command

Hi,

you should always quote substitution, hence:
case "$var" in
"$var2) ... ;;
esac

Just my $0.02,
Wodisch
Magdi KAMAL
Respected Contributor

Re: using variables in case command

Hi Gary,

You have to put the exact "Constant" into the value of case. You are NOT allowed to do it as you mentioned :

case $var1 in
$var2 )
do stuff ;;
* )
do nothing ;;
esac

Syntax would be :

case $var1 in
'001|002|003')
do stuff ;;
* )
do nothing ;;
esac

No varaibles are allowed to be used in the constant expressions.

Magdi
gary phipps
Frequent Advisor

Re: using variables in case command

Hi folks,

thanks for all the input. I tried all of the above but unfortunately none of them worked. I'll try various combinations of your suggestions and let you know what happens.


Gary.
Mark Greene_1
Honored Contributor

Re: using variables in case command

how are your populating var1 prior to doing the case?

mark
the future will be a lot like now, only later
Mark Fenton
Esteemed Contributor

Re: using variables in case command

Gary,

The syntax you need will be something like:

var1=$1
var2='00[1-3]'

case $var1 in
$var2) commands ;;
*) other commands ;;
esac

The issue is how you are assigning your variables. If you know what the allowable range of values for var2 is ahead of time, you can use the above style of assignment to test.

Mayhap if you could post how you're deriving var1 and var2 it would be helpful (if the above isn't what you're after).

Mark