< Day Day Up > |
5.5. while and untilThe remaining two flow control constructs bash provides are while and until. These are similar; they both allow a section of code to be run repetitively while (or until) a certain condition becomes true. They also resemble analogous constructs in Pascal (while/do and repeat/until) and C (while and do/until). while and until are actually most useful when combined with features we will see in the next chapter, such as integer arithmetic, input/output of variables, and command-line processing. Yet we can show a useful example even with what we have covered so far. The syntax for while is: while condition do statements... done For until, just substitute until for while in the above example. As with if, the condition is really a list of statements that are run; the exit status of the last one is used as the value of the condition. You can use a conditional with test here, just as you can with if. Note that the only difference between while and until is the way the condition is handled. In while, the loop executes as long as the condition is true; in until, it runs as long as the condition is false. The until condition is checked at the top of the loop, not at the bottom as it is in analogous constructs in C and Pascal. The result is that you can convert any until into a while by simply negating the condition. The only place where until might be more meaningful is something like this: until command ; do statements... done The meaning of this is essentially, "Do statements until command runs correctly." This is not a likely contingency. Here is an earlier task that can be rewritten using a while.
We can use the while construct and pattern matching to traverse the PATH list: path=$PATH: while [ $path ]; do ls -ld ${path%%:*} path=${path#*:} done The first line copies PATH to a temporary copy, path, and appends a colon to it. Normally colons are used only between directories in PATH; adding one to the end makes the code simple. Inside the while loop we display the directory with ls as we did in Task 5-2. path is then updated by removing the first directory pathname and colon (which is why we needed to append the colon in the first line of the script). The while will keep looping until $path expands to nothing (the empty string ""), which occurs once the last directory in path has been listed. Here is another task that is a good candidate for until.
Here is the code: until cp $1 $2; do echo 'Attempt to copy failed. waiting...' sleep 5 done This is a fairly simple use of until. First, we use the cp command to perform the copy for us. If it can't perform the copy for any reason, it will return with a non-zero exit code. We set our until loop so that if the result of the copy is not 0 then the script prints a message and waits five seconds. As we said earlier, an until loop can be converted to a while by the use of the ! operator: while ! cp $1 $2; do echo 'Attempt to copy failed. waiting...' sleep 5 done In our opinion, you'll seldom need to use until; therefore, we'll use while throughout the rest of this book. We'll see further use of the while construct in Chapter 7. |
< Day Day Up > |