Operating System - Linux
1828330 Members
4689 Online
109976 Solutions
New Discussion

Re: Linux shell scripting

 
Ragni Singh
Super Advisor

Linux shell scripting

Hello, all, I have this script with a whole bunch of "if" statements.

How is it possible to make it into a nested "if' statement or if there is such a thing.

Does anyone else have any idea how to make this a better script?
6 REPLIES 6
Stuart Browne
Honored Contributor

Re: Linux shell scripting

Well, nested is exactally as you think it should be:

if [ condition ]
then
if [ nextcondition ]
then
if [ anothercondition ]
then
code
fi
fi
fi

Or in-line:

if [ condition -a nextcondition -a anothercondition ]
then
code
fi

Or perhaps you'd prefer a case:

case value in
value1) code ;;
value2) code ;;
*) defaultcode ;;
esac
One long-haired git at your service...
Gopi Sekar
Honored Contributor

Re: Linux shell scripting


case is ideal for these kind of situation.

if that is not possible you can use if-elif-fi

eg:
if
then

elif
then

else

fi


Regards,
Gopi
Never Never Never Giveup
Steve_160
Frequent Advisor

Re: Linux shell scripting

Maybe a "case" statement might work better for you. It's hard to say without seeing the script.
#@%!! Windows!
Steve_160
Frequent Advisor

Re: Linux shell scripting

Oops. Sorry, Gopi. :-/ I didn't mean to repeat what you said. When the question first came up on the screen there were no replies shown.
#@%!! Windows!
Gopi Sekar
Honored Contributor

Re: Linux shell scripting


steve,
for that matter i indeed repeated stuart's suggestion :)

if you are not able to see the replies, then raise it as an issue to moderators
Never Never Never Giveup
Ross Minkov
Esteemed Contributor

Re: Linux shell scripting

See the Advanced Bash-Scripting Guide at http://www.tldp.org/LDP/abs/html/testconstructs.html for more examples.

-Ross