Shell Scripting
◾
277
7.6 LOOPS
7.6.1 Conditional Loops
The Bash scripting language has three types of loops:
conditional
loops,
counter-con-
trolled
loops, and
iterator
loops. A conditional loop is a
loop controlled by a condition, as
described in Section 7.4. There are two versions of the conditional loop: a while loop and
an until loop. They are both
pre-test
loops meaning that the condition is tested before the
loop body is executed. The difference between the two is in the semantics of the condition.
The while loop iterates
while
the condition is true. The until loop iterates
until
the condi-
tion becomes true.
The syntax of the two loops is shown below.
while [ condition ]; do
action(s);
done
until [ condition ]; do
action(s);
done
As with other Bash syntax, if you separate
the word do from the condition, then the
semicolon can be omitted. Additionally, if there are multiple actions, the semicolons sepa-
rating them can be omitted if the actions are on separate lines. The conditions must con-
form to the conditions we discussed in Section 7.4.
Let us examine an example illustrating both loops. The following
two loops will input
numbers from the user and sum those values together. The condition to exit the loop occurs
when a negative number is entered. The while loop’s condition is
[ $VALUE –ge 0 ]
while the until loop’s condition is the opposite,
[ $VALUE –lt 0 ]
.
SUM
=
0
read –p "Enter
the first number, negative to exit" VALUE
while [ $VALUE –ge 0 ]; do
SUM
=
$((SUM
+
VALUE))
read –p "Enter next number, negative to exit" VALUE
done
SUM
=
0
read –p "Enter the first number, negative to exit" VALUE
until [ $VALUE –lt 0 ]; do
SUM
=
$((SUM
+
VALUE))
read –p "Enter next number, negative to exit" VALUE
done
Conditional loops can be controlled by a number of different types of logic. For instance,
in the above, the user controls when to exit the loop by entering a negative number. This
278
◾
Linux with Operating System Concepts
is sometimes referred to as a
sentinel
loop in that a sentinel value
is used as a terminating
condition. Here, any negative number is the sentinel value. We can also control a condi-
tional loop by asking the user to respond to a prompt of whether to continue or to exit.
Below is a similar set of code to the two previous examples but requires the extra step of
asking the user if there are more numbers to input.
SUM
=
0
read –p "Do you have numbers to sum?" ANSWER
while [ $ANSWER
==
Yes ]; do
read –p "Enter the next number" VALUE
SUM
=
$((SUM
+
VALUE))
read –p "Do you have additional numbers?" ANSWER
done
Another way to control a conditional loop is to base the condition on the result of a compu-
tation. Below is a loop that will compute and output all of the powers of 2 less than 1000. When
the loop exits, VALUE will be greater than or equal to 1000 (in fact, it will be 1024), which is
not printed out because it is greater than the largest value that the code is to print out.
VALUE
=
1
while [ $VALUE –lt 1000 ]; do
echo $VALUE
VALUE
=
$((VALUE*2))
done
7.6.2
Counter-Controlled Loops
Another type of loop in Bash is the counter-controlled loop. This loop is a variation of the
C/Java for loop. In this loop, you specify three pieces of information: a variable
Do'stlaringiz bilan baham: