- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: nested if else statement
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2008 03:54 AM
11-17-2008 03:54 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2008 04:21 AM
11-17-2008 04:21 AM
Re: nested if else statement
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2008 04:22 AM
11-17-2008 04:22 AM
Re: nested if else statement
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2008 04:45 AM
11-17-2008 04:45 AM
Re: nested if else statement
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..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2008 04:52 AM
11-17-2008 04:52 AM
Re: nested if else statement
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2008 05:53 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2008 06:13 AM
11-17-2008 06:13 AM
Re: nested if else statement
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2008 08:38 PM
11-17-2008 08:38 PM
Re: nested if else statement
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-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2008 08:42 PM
11-17-2008 08:42 PM
Re: nested if else statement
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-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2008 09:12 PM
11-17-2008 09:12 PM
Re: nested if else statement
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2008 10:06 PM
11-17-2008 10:06 PM
Re: nested if else statement
Thanks to correct my typographical errors. And also thanks to tell me more about string comparisions.
-NKG-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2008 10:11 PM
11-17-2008 10:11 PM
Re: nested if else statement
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-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2008 12:15 AM
11-18-2008 12:15 AM
Re: nested if else statement
Many thanks,
I got solution first from Deninis reply.
Also thanks Nitin for expaling very nicely.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2008 12:20 AM
11-18-2008 12:20 AM
Re: nested if else statement
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2008 03:04 AM
11-18-2008 03:04 AM
Re: nested if else statement
This is not the case in a real shell. == is only valid for arithmetic expressions (( )).
(Unless this is new for ksh 93?)