The while loop
while condition
do commands
done
The shell first tests to see if condition is true.
If it is, the shell runs the commands. The shell then goes
back to check the condition. If it is still true, the shell
runs the commands again, and so on, until the condition is
found to be false.i=100
date
while test $i -gt 0
do
prog
let i=$i-1
done
date
The script begins by setting a variable i to 100.
It then uses the date command to get the current date and
time.Next the script runs a while loop. The test condition says that the loop should keep on going as long as the value of i is greater than zero. The commands of the loop run prog and then subtract 1 from the i variable. In this way, i goes down by 1 each time through the loop, until it is no longer greater than 0. At this point, the loop stops and the final instruction of the script prints out the date and time at the end of the loop. The difference between the starting time and the ending time should give some idea of how long it took to run the program 100 times.
(Of course, the shell itself takes some time to perform the test and to do the calculations with i. If prog takes a long time to run, the time spent by the shell is relatively unimportant; if prog is a quick program, the extra time that the shell takes may be large enough to make the timing incorrect.)
i=100
date
while let "(i=$i-1) >= 0"
do
prog
done
date
In this example, the let command is the condition
of the while loop. It gives i a new value and then
compares this value to zero. The advantage of this way of writing
the program is that it does not have to call test to make
the comparison; this speeds up the loop and makes the time more accurate.