Operating System - HP-UX
1827243 Members
2385 Online
109716 Solutions
New Discussion

Re: nested if else statement

 
SOLVED
Go to solution
diwakar_4
Frequent Advisor

nested if else statement

Hi all,

I want to execute if else in such a way:

Num=10
Char='Y'

if [ $Num = [0 OR 1 OR 2] ]; then
echo " Num is > 0 but < 3 "
else
if [ $Num = 3 OR 4] and $Char='Y' ];then
echo " num < 5 and char is : 'Y'
else
echo " num > 5 and char is : 'N'
fi;
fi;

Can some one please help me to right correctly these conditions.

Thanks


14 REPLIES 14
Ninad_1
Honored Contributor

Re: nested if else statement

Hi,

The following will do,

Num=3
Char='Y'

if [ $Num -ge 0 -a $Num -lt 3 ] ; then
echo " Num is > 0 but < 3 "
elif [ $Num -eq 3 -o $Num -eq 4 ] ; then
if [[ $Char = 'Y' ]] ; then
echo " num < 5 and char is : 'Y'"
fi
elif [[ $Char = 'N' ]] ; then
echo " num > 5 and char is : 'N'"
else
echo " num > 5 and char is : 'Y' "
fi


Regards,
Ninad
Fredrik.eriksson
Valued Contributor

Re: nested if else statement

I think this would be easier solved by using Case statement instead of if's. But I'm guessing you have a plan for this so here we go :).

if [ $Num -ge 0 -a $Num -lt 3 ]; then
echo "Num is > 0 but < 3"
else
if [ $Num -ge 3 -a $Num -le 4 ]; then
if [ "$Char" == "Y" ]; then
echo " num < 5 and char is : 'Y'"
else
echo " num < 5 and char is : 'N'"
fi
elif [ $Num -ge 5 ]; then
if [ "$Char" == "Y" ]; then
echo " num > 5 and char is : 'Y'"
else
echo " num > 5 and char is : 'N'"
fi
fi
fi

I made som modifications to yours since it didn't account for all the possible versions. And I added a elif :)

Best regards
Fredrik Eriksson
diwakar_4
Frequent Advisor

Re: nested if else statement

Hi Ninad/Fredrik,

Thanks,


if [ $Num = 3 OR 4] and $Char='Y' ];then

I want above condition in one line rathar than spiliting in many lines.

Can you suggets..
Ninad_1
Honored Contributor

Re: nested if else statement

Hi,

I do not see any benefit in putting everything in one line, except for code being a bit long.
Infact u should have the 2 conditions separate so as to also check for Char = N even if Num=3 or 4.

Regards,
Ninad
Dennis Handly
Acclaimed Contributor
Solution

Re: nested if else statement

>I want condition in one line rather than splitting in many lines.
if [ $Num = 3 OR 4] and $Char='Y' ];then

if [ \( $Num -eq 3 -o $Num -eq 4 \) -a "$Char" = "Y" ]; then
V. Nyga
Honored Contributor

Re: nested if else statement

Hi,

why don't you also post your errors you've got?
In sh-shell or ksh-shell OR is ||, AND is &&:

if [[ $Num = 0 || $Num = 1 ]]; then
if [[ $Num = 10 && $Char = 'Y' ]]; then

Are we doing your homework?

Volkmar

*** Say 'Thanks' with Kudos ***
Nitin Kumar Gupta
Trusted Contributor

Re: nested if else statement

Hi Diwakar,

The shell script will be,

Num=10
Char='Y'

if [ $Num -eq 0 -o $Num -eq 1 -o $Num -eq 2 ]; then
echo " Num is > 0 but < 3 "
else
if [ $Num -eq 3 -o $Num -eq 4 ]&[ "$Char" = "Y" ];then
echo " num < 5 and char is : 'Y'"
else
echo " num > 5 and char is : 'N'"
fi
fi

--------------------------------------------
# OR
Num=10
Char='Y'

if [ $Num -eq 0 -o $Num -eq 1 -o $Num -eq 2 ]; then
echo " Num is > 0 but < 3 "
elif [ $Num -eq 3 -o $Num -eq 4 ]&[ "$Char" = "Y" ];then
echo " num < 5 and char is : 'Y'"
else
echo " num > 5 and char is : 'N'"
fi



OK Rules of the test for shell script:
Comparison operator
-------------------
- Numeric: -eq -ne -lt -le -gt -ge
- String: = !=

Logical operator
----------------
Numeric: ! Not
-a And
-o Or
String: ! Not
&& And
|| Or

OK,
- If you want to make two numeric comparision with and
if [ Cond1 -a Cond2 ];then # Here Cond like $a -lt 5

- If you want to make and with or
if [ Cond1 -a Cond2 -o Cond3 ];then

- But you want to change the order of testing
if [ Cond1 -s \( Cond2 -a Cond3 \) ];then
#Here spaces are important

- You want to compare two strings with and or or

if [ Cond1 ]&&[ Cond2 ]||[Cond3 ];then

Similarly you want to compare integer comparision with string with and

if [ intCond ]&&[ stringCond ];then


I hope now you will be able to do all possible comarisions.

Rgds
-NKG-
Nitin Kumar Gupta
Trusted Contributor

Re: nested if else statement

Also,

for if, else, nested if else
1)
if ...
then
....
fi

2)
if ....
then
....
else
....
fi

3)
if ....
then
....
else
if ....
then
....
fi
fi

can be

if ....
then
....
elif ....
then
....
fi

I think you will be able to make all possible condition type.

Rgds
-NKG-
Dennis Handly
Acclaimed Contributor

Re: nested if else statement

>Nitin: elif [ $Num -eq 3 -o $Num -eq 4 ]&[ "$Char" = "Y" ];then

That should be "&&":
elif [ $Num -eq 3 -o $Num -eq 4 ] && [ "$Char" = "Y" ]; then
Or using arithmetic expressions:
elif (( Num == 3 && Num == 4 )) && [ "$Char" = "Y" ]; then

>You want to compare two strings with and or or
if [ Cond1 ]&&[ Cond2 ]||[Cond3 ]; then

There is no need to use multiple test operators, you can use -a or -o

>if [ Cond1 -s \( Cond2 -a Cond3 \) ];then

I assume that -s should be -o.
Nitin Kumar Gupta
Trusted Contributor

Re: nested if else statement

Hello Dennis,

Thanks to correct my typographical errors. And also thanks to tell me more about string comparisions.

-NKG-
Nitin Kumar Gupta
Trusted Contributor

Re: nested if else statement

And Diwakar,

Your solution would be,

Num=10
Char='Y'

if [ $Num -eq 0 -o $Num -eq 1 -o $Num -eq 2 ]; then
echo "Num is > 0 but < 3"
elif [ $Num -eq 3 -o $Num -eq 4 ]&&[ "$Char" = "Y" ];then
echo "num < 5 and char is : 'Y'"
else
echo "num > 5 and char is : 'N'"
fi


-NKG-
diwakar_4
Frequent Advisor

Re: nested if else statement

Hi Dennis/Nitin,

Many thanks,

I got solution first from Deninis reply.

Also thanks Nitin for expaling very nicely.

Thanks
Fredrik.eriksson
Valued Contributor

Re: nested if else statement

Nitin,
I would not use the -eq num method.
If he needs to do this with a greater amount of numbers then 3 it'd be a pain and a long long line.

It's better to use greater and lesser then comparision.

if [ $Num -ge 0 -a $Num -le 2 ]; then
fi

Also the string comparision operator is not =, it's ==. Also a note for diwakar, don't forget the encapsulation with "".

if [ "$Char" == "Y" ]; then
fi

Best regards
Fredrik Eriksson
Dennis Handly
Acclaimed Contributor

Re: nested if else statement

>Fredrik: Also the string comparison operator is not =, it's ==.

This is not the case in a real shell. == is only valid for arithmetic expressions (( )).
(Unless this is new for ksh 93?)