initializa-
tion
, a loop
continuation condition
, and a variable
increment
. The format of this instruc-
tion is
for ((
initialization
;
condition
;
increment
));
do
loop body
done
When this form of for loop executes, it first performs the initialization. This will ini-
tialize the variable that is to be used in both the condition and increment. The condition
is then tested. If the condition is true, the loop body executes and then the increment
instruction is performed. The condition is then retested and if true, the loop body executes
followed by the increment instruction. This repeats until the condition becomes false. For
those of you unfamiliar with C or Java, this for loop can be a challenge to use both syntacti-
cally and logically.
Shell Scripting
◾
279
What follows are several examples of the for loop statement (without the loop body
or do/done statements). The notation i
++
is a shortcut for saying i
=
i
+
1, or incrementing i
by 1.
•
for ((i=0; i < 100; i++)) –
iterate from 0 to 99
•
for((i
=
0; i
<
100; i
=
i
+
2))
– iterate from 0 to 99 by 2s
•
for ((i
=
100; i
>
j; i––))
– iterate from 100 down to j, whatever value j is storing
The syntax for the initialization, condition, and increment are similar to the syntax that
we saw earlier in conditions that used (()) instead of []. In this case, you can forego the $
preceding any variable name, you do not need to insert blanks around each portion of the
statement, you can use the relational operators (
<
,
>
=
, etc) rather than the two-letter con-
ditions, and you are free to use the shortcut operators such as
+ +
or
+
=
.
7.6.3 Iterator Loops
The final loop available in Bash is another for loop. In this case, it is an iterator loop. An
iterator loop is one that executes the loop body one time per item in a list. The iterator loop
uses the keyword for, but is more like a for each loop as found in C# and perl. The Bash for
loop has the form
for
VAR
in
LIST
do;
action(s)
done
The variable, VAR, takes on each element of the LIST one at a time. You can use the VAR
in the action(s). The variable VAR is often referred to as the loop variable.
The LIST can be an enumerated list such as (1 2 3 4 5), the result of a Linux com-
mand which generates a list (e.g., ls, cat), a list generated through filename expansion (for
instance *.txt would expand into all file names of.txt files in the current directory) or the
list of parameters supplied to the script using $@.
The following example will sum up the numbers passed to the script as parameters. The
variable VALUE obtains each parameters supplied by the user, one at a time. That value is
then added to the SUM. If this script were called
adder
, we might execute it using .
/adder
581 32 115 683 771
where the five numbers are used for the list in place of
$@
.
#!/bin/bash
SUM
=
0
for VALUE in $@; do
SUM
=
$((SUM
+
VALUE))
done
echo $SUM
In this example, notice how the loop variable, VALUE, is used in the loop body. If the
list that the loop variable iterates over consists of file names, we could apply file commands.
280
◾
Linux with Operating System Concepts
We might, for instance, wish to see the size of a series of files. The wc command will pro-
vide us this information.
#!/bin/bash
for filename in $@; do
wc $filename
done
To ensure that all of the parameters were actually files, we should enhance this script to
test each filename. We would use
[ -e $filename ]
to make sure the file exists. Our
new script is
#!/bin/bash
for filename in $@; do
if [ -e $filename ]; then
wc $filename
fi
done
Earlier, we introduced the instruction
shift
. Now that we have seen the use of
$@
, we
will reconsider shift. The easiest way to iterate through the parameters in a list is through
the for loop iterating over
$@
. For instance,
for value in $@; do . . .
However, we can also use shift and the counter-controlled for loop.
Below, we convert the earlier example of summing up the values in
$@
to use the
counter-controlled for loop and the shift instruction. Recall that shift will shift each
parameter down one position. So, if there are four parameters, stored in
$1
,
$2
,
$3
, and
$4
,
shift
will cause
$2
to shift into
$1
,
$3
into
$2
, and
$4
into
$3
. The original value
of
$1
is gone and now we have three values. Executing shift again will move
$2
into
$1
and
$3
into
$2
. In the script below, we first obtain the number of parameters, as stored
in
$#
. We now use a for loop to iterate that many times, using the value in
$1
as the cur-
rent parameter. The shift instruction then moves each parameter down so that there is a
new value in
$1
.
#!/bin/bash
NUMBER
=
$#
SUM
=
0
for ((i
=
0; i
<
NUMBER; count
+
+
)); do
SUM
=
$((SUM
+
$1))
shift
done
echo $SUM
Shell Scripting
◾
281
7.6.4 Using the Seq Command to Generate a List
We can also use the iterator version of the for loop as a counter-controlled loop. This is
done by replacing the list with a sequence generated by the
seq
command. The seq com-
mand returns a list of numbers based on between one and three parameters. The variations
available for seq are shown below.
•
Do'stlaringiz bilan baham: |