Operating System - HP-UX
1819901 Members
2606 Online
109607 Solutions
New Discussion юеВ

If Then Else Statement Help

 
SOLVED
Go to solution
Jeff Paciolla_1
Frequent Advisor

If Then Else Statement Help

This is probably a very simple question, but any help will be appreciated. Does anybody have an example of an if statement where the variable could be more than one value.

Here is an example one value that works.
******************************
NAME=$1

if [ $NAME = "John" ] ; then
echo $NAME
else
exit 0
fi
****************************

What i need to do is add another possible value in the if statement where $NAME could equal "John or "Jane".

I can't seem to get the syntax correct.

Thanks
Jeff
11 REPLIES 11
James R. Ferguson
Acclaimed Contributor
Solution

Re: If Then Else Statement Help

Hi Jeff:

How about this:

if [ "$I" = jim -o "$I" = james ]
then
echo "that's me!"
else
echo "I am not me"
fi

Regards!

...JRF...
Bill McNAMARA_1
Honored Contributor

Re: If Then Else Statement Help

#!/usr/bin/ksh

export NAME=$1

if [ $NAME = "John" ] ; then
echo "John $NAME "
elif [ $NAME = "Jane" ] ; then
echo "Jane: $NAME"
exit 0
fi

(man ksh / look for case if there is multiple options)
Later,
Bill
It works for me (tm)
Jeff Paciolla_1
Frequent Advisor

Re: If Then Else Statement Help

James - What does the -o do in the if statement?
Thanks
Jeff
James Beamish-White
Trusted Contributor

Re: If Then Else Statement Help

Hiya,

Try

if [ "$NAME" = "John" || "$NAME" = "Jane"] ; then
echo $NAME
else
exit 0
fi

I think && is the AND....

Note that the /sbin/init.d directory has loads of HP startup scripts that are a good start to look for syntax :-)

Cheers,
James
GARDENOFEDEN> create light
A. Clay Stephenson
Acclaimed Contributor

Re: If Then Else Statement Help

Hi Jeff:

if [ "${name}" = "John" -o "${name}" = "Jane" ]
then
echo "Ok"
else
echo "Bad"
fi

Note the quotes around "${name}" - they protect you in case the variable is ever null;

Here's a variant with numeric comparisons using and

if [ ${a} -ge 1 -a ${a} -le 10 ]
then
echo "${a} is in range"
else
echo "${a} is out of range"
fi
If it ain't broke, I can fix that.
Roger Baptiste
Honored Contributor

Re: If Then Else Statement Help

What i need to do is add another possible value in the if statement where $NAME could equal "John or "Jane". >

use -o option for giving the OR choice.
[ $NAME = "John" -o $NAME = "Jane" ]

everything else remaining the same.

(you can do a man on ksh for other related
options)

-raj

Take it easy.
S.K. Chan
Honored Contributor

Re: If Then Else Statement Help

This could work too ..

[[ $NAME = John || $NAME = Jane ]] && echo $NAME || exit

A one-liner without the "if" and the "then"
Victor BERRIDGE
Honored Contributor

Re: If Then Else Statement Help

>What does the -o do in the if statement?

-o stands For OR ... -a => AND

All the best

Victor
James R. Ferguson
Acclaimed Contributor

Re: If Then Else Statement Help

Hi Jeff:

The '-o' is "or". '-a' denotes "and". Have a look at the man pages for 'test' ('man test').

Regards!

...JRF...
mike boswell_3
New Member

Re: If Then Else Statement Help

"man test"

good advice
KapilRaj
Honored Contributor

Re: If Then Else Statement Help

Mike .... the last comment before urs was in 2001 !!!!!!!

kaps
Nothing is impossible