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 ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.