while and until

5.5 while and until

The 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 .

Task 5-6

Reimplement Task 5-2 without the use of the IFS variable.

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 .

Task 5-7

Write a script that attempts to copy a file to a directory and, if it fails, waits five seconds, then tries again, continuing until it succeeds.

Here is the code:

  until cp  ; 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  ; 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 .

 



Learning the Bash Shell
Learning the bash Shell: Unix Shell Programming (In a Nutshell (OReilly))
ISBN: 0596009658
EAN: 2147483647
Year: 1998
Pages: 104

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net