This section describes other important Bourne shell and
bash control structures.
while statement is one
way to create a loop. It has two forms:
while form, the
commands are executed until the
condition becomes false. In the
until form, they are executed until
the condition becomes true. Here
is an example of
cat /etc/fstab | while read DEVICE MOUNT_DIR READONLY FS DUMMY1 DUMMY2 do fsck (if required) and mount the device done
This loop takes each line of /etc/fstab in turn (sent to it via
cat) and performs an appropriate action for
the corresponding device. The
loop will end when
later) returns a nonzero status, indicating an end-of-file.
Here is another very similar example, taken from a recent Linux system:
while read des fs type rest; do case "$fs" in /) break;; *) ;; esac done < /etc/fstab if [ -e "$des" -a "$type" != "resiserfs" ] then run fsck fi
Note that the input to the while loop is provided via I/O
redirection following the
case command is a way to
perform a branching operation. Here is its syntax:
The value in str is compared against each of the patterns. The corresponding commands are executed for the first match that is found. The double semicolons are used to end each section. Wildcards ...