O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

18.3. if then else statements

The if statement lets you test for a condition. Based on the true (0) or false (1) return of a test, a series of statements can then be performed. The construct of the if statement makes it ideal for error checking. Here’s the format of the if statement:

if condition1 then   commands1 elif condition2 then   commands2 else   commands3 fi 

Let’s break down this if statement to see what’s really going on.

if condition1 If condition1 is true
then then
commands1 do the commands1
elif condition2 if condition1 is not met
then then
commands2 do the commands2
else if condition1 or condition2 is not met
commands3 then do commands3
fi finish

The if statement must be terminated with the word fi. Leaving out the closing fi ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required