Register
It is currently Mon Nov 24, 2014 6:36 pm

Basic if/then expressions


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Mon May 16, 2005 11:33 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
If-then

if [ expression ]
then
commands
fi
--------------------------------

If-then-else

if [ expression ]
then
commands
else
commands
fi
--------------------------------

If-then-else If-else

if [ expression ]
then
commands
elif [ expression2 ]
then
commands
else
commands
fi


Top
 Profile WWW  
 PostPosted: Mon May 16, 2005 11:33 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 673
Location: Des Moines, Iowa
Posted by jbsnake

crouse wrote:

If-then-else If-else

if [ expression ]
then
commands
elif [ expression2 ]
then
commands
else
commands
fi

instead of calling it If-else...i believe it should be else-if...because it's incased within the first if :)
just my thoughts on it


Top
 Profile WWW  
 PostPosted: Tue May 17, 2005 4:11 pm   

Joined: Tue May 17, 2005 4:06 pm
Posts: 4
Location: Penguin Island, Antarctica
Just to add that if statements in bash are highly space sensitive. You need to have a space between

if and [ and then a space between [ and the condition and then a space between the condition and ]. So, if I were to test somethind like this:

if [ -d $1 ];
then
do
ls;
fi

^ something like that would be correct syntax.


Top
 Profile WWW  
 PostPosted: Fri Oct 19, 2007 3:21 pm   

Joined: Fri Oct 19, 2007 3:09 pm
Posts: 3
Don't anger the gods, use quotes! :)
Code:
if [ -d "$1" ]; then
...
fi


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP