Previous topic |
Next topic |
Contents |
Contact z/OS |
Library |
PDF
The while loop z/OS UNIX System Services User's Guide SA23-2279-00 |
|
The while loop repeats one or more commands while a particular
condition is true. The loop has the form:
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.As an example of how this can be used, suppose you want to run
a program named prog 100 times to get an idea
of the program's average running speed. The following shell script
does the job:
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.) You can rewrite this shell script to make it a little more efficient:
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. |
Copyright IBM Corporation 1990, 2014
|