You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />
case EXPRESSION in CASE1) COMMAND-LIST;; CASE2) COMMAND-LIST;; ... CASEN)<br />
COMMAND-LIST;; esac<br />
Each case is an expression matching a pattern. The commands in the COMMAND-LIST for the first match<br />
are executed. The "|" symbol is used for separating multiple patterns, and the ")" operator terminates a pattern<br />
list. Each case plus its according commands are called a clause. Each clause must be terminated with ";;".<br />
Each case statement is ended with the esac statement.<br />
In the example, we demonstrate use of cases for sending a more selective warning message with the<br />
disktest.sh script:<br />
anny ~/testdir> cat disktest.sh<br />
#!/bin/bash<br />
# This script does a very simple test for checking disk space.<br />
space=`df -h | awk '{print $5}' | grep % | grep -v Use | sort -n | tail -1 | cut -d "%" -f1 -`<br />
case $space in<br />
[1-6]*)<br />
Message="All is quiet."<br />
;;<br />
[7-8]*)<br />
Message="Start thinking about cleaning out some stuff. There's a partition that is $space % fu<br />
;;<br />
9[1-8])<br />
Message="Better hurry with that new disk... One partition is $space % full."<br />
;;<br />
99)<br />
Message="I'm drowning here! There's a partition at $space %!"<br />
;;<br />
*)<br />
Message="I seem to be running with an nonexistent amount of disk space..."<br />
;;<br />
esac<br />
echo $Message | mail -s "disk report `date`" anny<br />
anny ~/testdir><br />
You have new mail.<br />
anny ~/testdir> tail -16 /var/spool/mail/anny<br />
From anny@octarine Tue Jan 14 22:10:47 2003<br />
Return-Path: <br />
Received: from octarine (localhost [127.0.0.1])<br />
by octarine (8.12.5/8.12.5) with ESMTP id h0ELAlBG020414<br />
for ; Tue, 14 Jan 2003 22:10:47 +0100<br />
Received: (from anny@localhost)<br />
by octarine (8.12.5/8.12.5/Submit) id h0ELAltn020413<br />
for anny; Tue, 14 Jan 2003 22:10:47 +0100<br />
Date: Tue, 14 Jan 2003 22:10:47 +0100<br />
From: Anny <br />
Message-Id: <br />
To: anny@octarine<br />
Subject: disk report Tue Jan 14 22:10:47 CET 2003<br />
Start thinking about cleaning out some stuff. There's a partition that is 87 % full.<br />
anny ~/testdir><br />
Chapter 7. Conditional statements 91