1848509 Members
6710 Online
104030 Solutions
New Discussion

a scripting question

 
Curtis Larson_1
Valued Contributor

a scripting question

how do you parmaterize the pattern in a case statement for sh or ksh. ie:

#!yourshell
pattern="ab|bb"
c="ab"
case $c in
$pattern) print $c;;
ab) print here;;
*) print "not found";;
esac

sh or ksh will return "here". how do I code this example to return "ab", which is what it does when using the /usr/dt/bin/dtksh shell.
6 REPLIES 6
Sridhar Bhaskarla
Honored Contributor

Re: a scripting question

Hi Curtis,

I can think of one work around it works for you.

#!/usr/bin/ksh
pattern="ab|bb"
c="ab"
cat << EOF > /tmp/sh$$
case $c in
$pattern ) print $c;;
ab) print here;;
*) print "not found";;
esac

EOF

ksh /tmp/sh$$
rm /tmp/sh$$

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Bernie Vande Griend
Respected Contributor

Re: a scripting question

Sridhar idea would work and it also shows why this doesn't ordinarily work. That's because in ksh and sh, you can't use a variable to substitute a pattern match. It will just try to pattern match the literal 'ab|bb', instead of substituting and then trying to use the logic within. This also happens if you try to do this same thing with arrays. I don't believe there's any way around this other than causing it to be evaluated twice like Sridhar does.
Ye who thinks he has a lot to say, probably shouldn't.
Rodney Hills
Honored Contributor

Re: a scripting question

Perl offers that ability. Although it has no case statement, you can do the following-

$regexp="ab|b";
...
if (/$regexp/) {
...
}

Within ksh you might be able to use the "eval" shell command (if it is a small list of case statements). eg-
x="ab|b"
val="b"
eval "case $val in $x) echo yes;; *) echo no;; esac"

-- Rod Hills
There be dragons...
Marco Paganini
Respected Contributor

Re: a scripting question

Hello Curtis,

This workaround should work (without intermediate files)

#!/bin/sh

pattern="ab|bb"
c="ab"
eval "case $c in
$pattern) print $c;;
ab) print here;;
*) echo \"not found\";;
esac"

Regards,
Paga
Keeping alive, until I die.
Curtis Larson_1
Valued Contributor

Re: a scripting question

thanks everyone for your suggestions

i believe using the eval within the if-then-else construct to more useable for my programming.

if eval [[ $c = @($pattern) ]] ;then
print $c
elif eval [[ $c = "ab" ]] ;then
print here
else
print "not found"
fi
Sridhar Bhaskarla
Honored Contributor

Re: a scripting question

Interesting thing about the way you assigned points. I honor your judgement of giving me 3 points as this solution will not suit to your needs.

However, it would have been nice if you had given your program completely so that we might have suggested to use if-then-else construct instead of wasting our time to think about a workaround.

We only can think about the solutions based on the information you gave.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try