home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 45.18 Using basename and dirname Chapter 45
Shell Programming for the Initiated
Next: 45.20 Overview: Open Files and File Descriptors
 

45.19 A while Loop with Several Loop Control Commands

Most people think the Bourne shell's while loop ( 44.10 ) looks like this, with a single command controlling the loop:

while 

command


do
     
...whatever

done

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:

while echo "Enter command or CTRL-d to quit: \c"
      read command
do
      
...process 

$command

done

Here's a loop that runs who and does a quick search on its output. If the grep returns non-zero status (because it doesn't find $who in $tempfile ), the loop quits - otherwise, the loop does lots of processing:

while
    who > $tempfile
    grep "$who" $tempfile >/dev/null

do
    
...process $tempfile...

done

- JP


Previous: 45.18 Using basename and dirname UNIX Power Tools Next: 45.20 Overview: Open Files and File Descriptors
45.18 Using basename and dirname Book Index 45.20 Overview: Open Files and File Descriptors

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System










??????????????@Mail.ru