Skip to Content
Essential System Administration, 3rd Edition
book

Essential System Administration, 3rd Edition

by Æleen Frisch
August 2002
Beginner
1176 pages
36h 52m
English
O'Reilly Media, Inc.
Content preview from Essential System Administration, 3rd Edition

Other Control Structures

This section describes other important Bourne shell and bash control structures.

The while and until Commands

The while statement is one way to create a loop. It has two forms:

while condition
do
   commands
done

until condition
do
   commands
done

In the 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 while:

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 while loop will end when read (described 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 done statement.

The case Command

The case command is a way to perform a branching operation. Here is its syntax:

case str in 
   pattern_1)
     commands 
     ;; 
  
   pattern_2) 
     commands 
     ;; 
   ...
   pattern_n) 
     commands 
     ;; 
  
   *) 
     commands 
     ;; 
esac

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Unix® and Linux® System Administration Handbook, Fourth Edition

Unix® and Linux® System Administration Handbook, Fourth Edition

Evi Nemeth, Garth Snyder, Trent R. Hein, Ben Whaley

Publisher Resources

ISBN: 0596003439Errata Page