268
◾
Linux with Operating System Concepts
7.5.3 The If-Then and If-Then-Else Statements
Now that we have viewed the types of comparisons available, we can apply them in selec-
tion statements. The first type of selection statement
is the if-then statement, or a one-way
selection. If the condition is true, the
then clause
is executed, otherwise the
then clause
is
skipped. The syntax for the if-then statement is as follows.
if [
condition
]; then
action(s)
; fi
The syntax is somewhat bizarre. First, there must be a semicolon after the ] after the
condition. Second,
if there are multiple actions, they must be separated by semicolons.
Finally, the statement must end with the word
fi
. However, the use of the semicolons is
only needed if we place the entire instruction on a single line. If we separate each part onto
a separate line, the semicolons are not needed. Here are several examples.
•
if [ –e file1.sh ]; then ./file1.sh; fi
• if the file exists, execute it
•
if [ $X –gt 0 ]; then COUNT=$((COUNT+1)); fi
• if the value in X is greater than 0 add 1
to COUNT
•
if [ $NAME == $USER ]; then echo Hello master, I am ready
for you; fi
• specialized greeting if the value in NAME is equal to the user’s login
•
if [[ $X –ne 100 && $USER != Frank ]]; then X=0; Y=100;
NAME=Joe; fi
Let us rewrite the last two of these examples as they might appear in a script file.
if [ $NAME
==
$USER ]
then
COUNT
=
$((COUNT
+
1))
fi
if [[ $X –ne 100 && $USER !
=
Frank ]]
then
X
=
0
Y
=
100
NAME
=
Joe
fi
Notice that the indentation above is strictly for our own readability and not required
by the Bash interpreter. The following three sets of code are equivalent as far as the Bash
interpreter is concerned.
Shell Scripting
◾
269
•
if [ $X –gt $Y ];
then echo greater; X=0; fi
•
if [ $X –gt $Y ]
then
echo greater
X=0
fi
•
if [ $X –gt $Y ]
then
echo greater
X=0
fi
Recall from the last subsection, we noted that using the relational operators (e.g.,
<
,
>
=
)
in place of the two-letter abbreviations can yield an improper output. Consider the follow-
ing script.
#!/bin/bash
X
=
0
Y
=
1
if [ $X
>
$Y ]; then echo greater; fi
if [ $X
==
0 ]; then echo zero; fi
This script, when run,
outputs
greater
zero
There is no logical reason why greater should be output as it is clearly incorrect. For $X,
we are testing it to 0 using the string comparison
==
(rather than the numeric compari-
son –eq). However, X does store 0, which can be interpreted as a string or a number, so the
output of zero does make sense.
In each of the examples covered so far, if the condition is true, the action(s)
is performed
and if not, the statement ends and the interpreter moves on to the next instruction. In
many situations, we have an alternative action that we would like executed if the condi-
tion is false. For this, we need a two-way selection statement, or an if-then-else
*
statement.
The difference between the if-then and if-then-else statements is the inclusion of the word
“else” and an
Do'stlaringiz bilan baham: