预计阅读本页时间:-
Arithmetic for Loops
Chapter 5 introduced the for loop and briefly mentioned another type of for loop, more akin to the construct found in many programming languages like Java and C. This type of for loop is called an arithmetic for loop.[14]
The form of an arithmetic for loop is very similar to those found in Java and C:
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
for (( initialisation ; ending condition ; update ))
do
statements...
done
There are four sections to the loop, the first three being arithmetic expressions and the last being a set of statements just as in the standard loop that we saw in the last chapter.
The first expression, initialisation, is something that is done once at the start of the loop and if it evaluates to true the loop continues its process; otherwise, it skips the loop and continues with the next statement. When initialisation is true the loop then evaluates ending condition. If this is true then it executes statements, evaluates update and repeats the cycle again by evaluation ending condition. The loop continues until ending condition becomes false or the loop is exited via one of the statements.
Usually initialisation is used to set an arithmetic variable to some initial value, update updates that variable, and ending condition tests the variable. Any of the values may be left out in which case they automatically evaluate to true. The following simple example:
for ((;;))
do
read var
if [ "$var" = "." ]; then
break
fi
done
loops forever reading lines until a line consisting of a . is found. We'll look at using the expressions in an arithmetic for loop in our next task.
Task 6-2
Write a script that uses for loops to print out a multiplication table for the numbers 1 to 12.
This task is best accomplished using nested for loops:
for (( i=1; i <= 12 ; i++ ))
do
for (( j=1 ; j <= 12 ; j++ ))
do
echo -ne "$(( j * i ))\t"
done
echo
done
The script begins with a for loop using a variable i; the initialisation clause sets i to 1, the ending condition clause tests i against the limit (12 in our case), and the update clause adds 1 to i each time around the loop. The body of the loop is another for loop, this time with a variable called j. This is identical to the i for loop except that j is being updated.
The body of the j loop has an echo statement where the two variables are multiplied together and printed along with a trailing tab. We deliberately don't print a newline (with the -n option to echo) so that the numbers appear on one line. Once the inner loop has finished a newline is printed so that the set of numbers starts on the next line.
Arithmetic for loops are useful when dealing with arrays, which we'll now look at.
[8] You can also use the older form $[...], but we don't recommend this because it will be phased out in future versions of bash.
[9] The assignment forms of these operators are also permitted. For example, $((x += 2)) adds 2 to x and stores the result back in x.
[11] ++ and - are not available in versions of bash prior to 2.04.
[12] Note that the truth values returned by $((...)) are 1 for true, 0 for false—the reverse of the test and exit statuses.
[13] ((...)) is not available in versions of bash prior to 2.0.
[14] Versions of bash prior to 2.04 do not have this type of loop.