PDA

View Full Version : BASH: Nesting "if"s?



LadyDoor
August 25th, 2006, 07:16 PM
Is there a special syntax required to nest "if" statements in BASH? Basically, I want to set it up in this script I'm working on so that, if one thing has a certain value, then it should go one and do one of two things depending on whether something else has a given value, and if not to just do nothing...something like this (with dummy variables and numbers)



if (($statmente1 == 1))
then

if (($statement2 == 17))
then
command -options file
elif (($statement2 == 3))
then
othercommand
else
yetanothercommand

else
echo $statement1 > /dev/null


Basically, I want to do this because it'll save me a lot of "&&" statements. Any suggestions?

--Door

chonger
August 25th, 2006, 07:56 PM
Sure. You can nest flow control statements in bash.

http://www.linuxvalley.it/encyclopedia/ldp/guide/abs/loops.html

LadyDoor
August 26th, 2006, 12:33 AM
Shiny. It turns out I just had a mistake in what I was writing, which was what was preventing it from working (I left out a fi).

--Door

goatflyer
August 26th, 2006, 12:55 AM
Shiny. It turns out I just had a mistake in what I was writing, which was what was preventing it from working (I left out a fi).

--Door

Err... you left out TWO 'fi's :P

LadyDoor
August 26th, 2006, 01:27 AM
Oh...lol. I just forgot to copy/paste the second one (the one I *didn't* miss) in. Thanks, though!