1826498 Members
1888 Online
109692 Solutions
New Discussion

Shell Programming

 
SOLVED
Go to solution
Scott E Smith
Frequent Advisor

Shell Programming

Hello all...
I am attempting to ensure that the value of a given variable matches one of three strings.
Basically it's
if "$PIE" does not equal "cherry" "apple" or "pumpkin" then i want to exit the loop and print the usage statement. If it does match then i want the loop to continue.
My syntax is as follows but does not work, I think is may be an -0 or || compund expression issue.... Thanks.
#!/usr/bin/sh
PIE=cherry

if [ "$PIE" != [ "apple" || "cherry" || "pumpkin" ]]
then
echo "\n****Valid names are apple,cherry or pumpkin!\n" ; exit 1
else
echo "\n****Name is valid!\n"

Thanks in advance for the help....







18 REPLIES 18
David Child_1
Honored Contributor
Solution

Re: Shell Programming

Try:

case $PIE in
apple) echo "\n****Name is valid!\n" ;;
cherry) echo "\n****Name is valid!\n" ;;
pumpkin) echo "\n****Name is valid!\n" ;;
*) echo "\n****Valid names are apple,cherry or pumpkin!\n" ; exit 1 ;;
esac

James R. Ferguson
Acclaimed Contributor

Re: Shell Programming

Hi Scott:

...
# if [ "$PIE" != "apple" -a "$PIE" != "cherry" -a "$PIE" != "pumpkin" ]; then
> echo "bad pie!"
> else
> echo "yummy pie!"
> fi

Regards!

...JRF...
Stuart Abramson_2
Honored Contributor

Re: Shell Programming

I don't think that "if" works that way.

I would probably do that with "case":

case $PIE in
pumpkin) print "Thank you.";;
cherry | apple) print "Gracias.";;
*) print "Wrong answer. Sorry..."
exit;;
esac
# Process pie


statements...
Dario_1
Trusted Contributor

Re: Shell Programming

Hi!

Try the following:

if [ "$PIE" != "apple" -o "$PIE" != "cherry" -o "$PIE" != "pumpkin" ]
then
echo "Not good"
else
echo "Goood!"
fi

Regards,

Dario
Hai Nguyen_1
Honored Contributor

Re: Shell Programming

You can also do this:

if [ $PIE = apple -o $PIE = cherry -o $PIE = pumpkin ]
then
echo true
else
echo wrong
fi
Michael Kelly_5
Valued Contributor

Re: Shell Programming

Scott,
this is a logic problem.
The logic says
"If $PIE is not equal to "apple" OR if $PIE is not equal to "cherry" OR if $PIE is not equal to "pumpkin"
then
echo "\n****Valid names are apple,cherry or pumpkin!\n" ; exit 1
else ...

As written, $PIE is not equal to "apple" so the test is TRUE and because it is TRUE, the shell will not evaluate the rest of the tests and it executes the first echo command.
The way to do this is by using an AND operator.

Here is a version that will do what you want.

#!/bin/sh
PIE=cherry

if [ "$PIE" != "apple" -a $PIE != "cherry" -a $PIE != "pumpkin" ]
then
echo "\n****Valid names are apple,cherry or pumpkin!\n" ; exit 1
else
echo "\n****Name is valid!\n"
fi

HTH,
Michael.
The nice thing about computers is that they do exactly what you tell them. The problem with computers is that they do EXACTLY what you tell them.
James R. Ferguson
Acclaimed Contributor

Re: Shell Programming

Hi:

Do *not* [no pun intended] use the 'or' operator [ -o ] with negatated equality checks. You will not achieve the desired outcome. Try this:

# PIE=apple

# [ "$PIE" != "apple" -a "$PIE" != "cherry" -a "$PIE" != "pumpkin" ] && echo BAD_PIE!

...now change '-a' to '-o':

[ "$PIE" != "apple" -o "$PIE" != "cherry" -o "$PIE" != "pumpkin" ] && echo BAD_PIE!

Regards!

...JRF...
John Meissner
Esteemed Contributor

Re: Shell Programming



if [ "$PIE" != "apple"] || [ $"PIE" != "cherry" ] || [ "$PIE" != "pumpkin" ] ;
then
echo "Usage: `basename $0` {whatever options}"
exit
else
#whatever commands you wnat
fi
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: Shell Programming

looking at everyone's replies... isn't it interesting how many differnt approaches there are as well as the ones we choose to take?

All paths lead to destiny
Ian Dennison_1
Honored Contributor

Re: Shell Programming

Why not just got for a burger? :)

Null points pse. Ian
Building a dumber user
Scott E Smith
Frequent Advisor

Re: Shell Programming

You guys all kick A$$..... Thanks a bunch. Ian you get points for humor...
James R. Ferguson
Acclaimed Contributor

Re: Shell Programming

Hi John M:

Your example should be use "&&" not "||" as:

# if [ "$PIE" != "apple" ] && [ "$PIE" != "cherry" ] && [ "$PIE" != "pumpkin" ]
> then
> echo BAD_PIE
> else
> echo GOOD_PIE
> fi

Regards!

...JRF...
Dario_1
Trusted Contributor

Re: Shell Programming

James:

Thank you for shaking my head. I used your approach (trying to avoid the typing) but using the -o option and forgot to remove the exclamation (!). You are totally correct....Thank you for the correction.

Regards,

Dario
John Meissner
Esteemed Contributor

Re: Shell Programming

James,
No - my example is correct. He's asking if pie does not equal apple OR pumpkin - hence the || operator. Whereas if he was checking to see if pie does not equal applor AND pumpkin then i'd use the &&
All paths lead to destiny
Donny Jekels
Respected Contributor

Re: Shell Programming

Scott,

use the shell's built in test operator.

the next if statement calls an external test operator;

if [ $PIE = xxx ]; then blabla
fi

for faster processing use the internal test operator

if [[ "$PIE" != "apple" -a "$PIE" != "cherry" -a "$PIE" != "pumpkin" ]]


"Vision, is the art of seeing the invisible"
Bill Douglass
Esteemed Contributor

Re: Shell Programming

Case-insensitive example:

#!/bin/sh
PIE=$1
if echo $PIE | egrep -i -q -e "^apple$|^cherry$|^pumpkin$"
then
echo "$PIE pie; umm-umm-good"
else
echo "I don't know about $PIE pie"
fi
Jean-Louis Phelix
Honored Contributor

Re: Shell Programming

Hi,

This variant uses 'select' : you have to choose a number instead of a fruit. See 'select' in man sh-posix.

Regards.

#!/usr/bin/sh
export PS3="Enter Fruit "
select VAR in apple cherry pumpkin
do
[ -z "$VAR" ] &&
{
echo "$REPLY is note a correct choice, exiting ..."
break
}
echo "You've chosen a $VAR"
done
It works for me (© Bill McNAMARA ...)
Chris Vail
Honored Contributor

Re: Shell Programming

The best solution, as pointed out by others is to use the case statement. But since you also asked for other solutions, here's a wierd one:

echo $PIE|egrep '^(apple|cherry|pumpkin)' 2>&1 > /dev/null
if test "$?" ne "0"
then
echo "ICKYPOO!!! I don't like $PIE pie!"
else
echo "Yummmm: a $PIE pie!"
fi

Chris<--regexps are your friend......
(if sometimes a very confusing friend)