home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX in a Nutshell: System V Edition

UNIX in a Nutshell: System V EditionSearch this book
Previous: Reference: history Chapter 4
The Bourne Shell and Korn Shell
Next: Reference: integer
 

if



if

 

condition

1


then

 

commands1

 
[ 

elif

 

condition2


	

then

 

commands2

 ]
 .
 .
 .
[ 

else

 

commands3

 ]


fi

If condition1 is met, do commands1 ; otherwise, if condition2 is met, do commands2 ; if neither is met, do commands3 . Conditions are usually specified with the test command. See additional examples under : and exit .

Examples

Insert a 0 before numbers less than 10:

if [ $counter -lt 10 ]
then number=0$counter
else number=$counter
fi

Make a directory if it doesn't exist:

if [ ! -d $dir ]; then
   mkdir $dir
   chmod 775 $dir
fi


Previous: Reference: history UNIX in a Nutshell: System V Edition Next: Reference: integer
Reference: history Book Index Reference: integer

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System