36.14. A while Loop with Several Loop Control CommandsI used to think that the Bourne shell's while loop (Section 35.15) looked like this, with a single command controlling the loop:
But command can actually be a list of commands. The exit status of the last command controls the loop. This is handy for prompting users and reading answers. When the user types an empty answer, the read command returns "false" and the loop ends:
You may need a -e option to make echo treat escaped characters like \c the way you want. In this case, the character rings the terminal bell, however your terminal interprets that (often with a flash of the screen, for instance.) Here's a loop that runs who and does a quick search on its output. If the grep returns nonzero status (because it doesn't find $who in $tempfile), the loop quits -- otherwise, the loop does lots of processing:
--JP and SJC
Copyright © 2003 O'Reilly & Associates. All rights reserved. |
|