old-www/LDP/Bash-Beginners-Guide/html/sect_07_03.html

387 lines
6.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Using case statements</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Bash Guide for Beginners"
HREF="index.html"><LINK
REL="UP"
TITLE="Conditional statements"
HREF="chap_07.html"><LINK
REL="PREVIOUS"
TITLE="More advanced if usage"
HREF="sect_07_02.html"><LINK
REL="NEXT"
TITLE="Summary"
HREF="sect_07_04.html"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Bash Guide for Beginners</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="sect_07_02.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 7. Conditional statements</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="sect_07_04.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="sect_07_03"
></A
>7.3. Using case statements</H1
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_07_03_01"
></A
>7.3.1. Simplified conditions</H2
><P
>Nested <B
CLASS="command"
>if</B
> statements might be nice, but as soon as you are confronted with a couple of different possible actions to take, they tend to confuse. For the more complex conditionals, use the <B
CLASS="command"
>case</B
> syntax:</P
><P
><B
CLASS="command"
>case <TT
CLASS="function"
>EXPRESSION</TT
> in <TT
CLASS="function"
>CASE1</TT
>) COMMAND-LIST;; <TT
CLASS="function"
>CASE2</TT
>) COMMAND-LIST;; ... <TT
CLASS="function"
>CASEN</TT
>) COMMAND-LIST;; esac</B
> </P
><P
>Each case is an expression matching a pattern. The commands in the <B
CLASS="command"
>COMMAND-LIST</B
> for the first match are executed. The <SPAN
CLASS="QUOTE"
>"|"</SPAN
> symbol is used for separating multiple patterns, and the <SPAN
CLASS="QUOTE"
>")"</SPAN
> operator terminates a pattern list. Each case plus its according commands are called a <EM
>clause</EM
>. Each clause must be terminated with <SPAN
CLASS="QUOTE"
>";;"</SPAN
>. Each <B
CLASS="command"
>case</B
> statement is ended with the <B
CLASS="command"
>esac</B
> statement.</P
><P
>In the example, we demonstrate use of cases for sending a more selective warning message with the <TT
CLASS="filename"
>disktest.sh</TT
> script:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>anny ~/testdir&#62;</TT
> <B
CLASS="command"
>cat <TT
CLASS="filename"
>disktest.sh</TT
></B
>
#!/bin/bash
# This script does a very simple test for checking disk space.
space=`df -h | awk '{print $5}' | grep % | grep -v Use | sort -n | tail -1 | cut -d "%" -f1 -`
case $space in
[1-6]*)
Message="All is quiet."
;;
[7-8]*)
Message="Start thinking about cleaning out some stuff. There's a partition that is $space % full."
;;
9[1-8])
Message="Better hurry with that new disk... One partition is $space % full."
;;
99)
Message="I'm drowning here! There's a partition at $space %!"
;;
*)
Message="I seem to be running with an nonexistent amount of disk space..."
;;
esac
echo $Message | mail -s "disk report `date`" anny
<TT
CLASS="prompt"
>anny ~/testdir&#62;</TT
>
You have new mail.
<TT
CLASS="prompt"
>anny ~/testdir&#62;</TT
> <B
CLASS="command"
>tail <TT
CLASS="parameter"
><I
>-16</I
></TT
> <TT
CLASS="filename"
>/var/spool/mail/anny</TT
></B
>
From anny@octarine Tue Jan 14 22:10:47 2003
Return-Path: &#60;anny@octarine&#62;
Received: from octarine (localhost [127.0.0.1])
by octarine (8.12.5/8.12.5) with ESMTP id h0ELAlBG020414
for &#60;anny@octarine&#62;; Tue, 14 Jan 2003 22:10:47 +0100
Received: (from anny@localhost)
by octarine (8.12.5/8.12.5/Submit) id h0ELAltn020413
for anny; Tue, 14 Jan 2003 22:10:47 +0100
Date: Tue, 14 Jan 2003 22:10:47 +0100
From: Anny &#60;anny@octarine&#62;
Message-Id: &#60;200301142110.h0ELAltn020413@octarine&#62;
To: anny@octarine
Subject: disk report Tue Jan 14 22:10:47 CET 2003
Start thinking about cleaning out some stuff. There's a partition that is 87 % full.
<TT
CLASS="prompt"
>anny ~/testdir&#62;</TT
>
</PRE
></FONT
></TD
></TR
></TABLE
><P
>Of course you could have opened your mail program to check the results; this is just to demonstrate that the script sends a decent mail with <SPAN
CLASS="QUOTE"
>"To:"</SPAN
>, <SPAN
CLASS="QUOTE"
>"Subject:"</SPAN
> and <SPAN
CLASS="QUOTE"
>"From:"</SPAN
> header lines.</P
><P
>Many more examples using <B
CLASS="command"
>case</B
> statements can be found in your system's init script directory. The startup scripts use <B
CLASS="command"
>start</B
> and <B
CLASS="command"
>stop</B
> cases to run or stop system processes. A theoretical example can be found in the next section.</P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_07_03_02"
></A
>7.3.2. Initscript example</H2
><P
>Initscripts often make use of <B
CLASS="command"
>case</B
> statements for starting, stopping and querying system services. This is an excerpt of the script that starts <SPAN
CLASS="application"
>Anacron</SPAN
>, a daemon that runs commands periodically with a frequency specified in days.</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;case "$1" in
start)
start
;;
stop)
stop
;;
status)
status anacron
;;
restart)
stop
start
;;
condrestart)
if test "x`pidof anacron`" != x; then
stop
start
fi
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
exit 1
esac
</PRE
></FONT
></TD
></TR
></TABLE
><P
>The tasks to execute in each case, such as stopping and starting the daemon, are defined in functions, which are partially sourced from the <TT
CLASS="filename"
>/etc/rc.d/init.d/functions</TT
> file. See <A
HREF="chap_11.html"
>Chapter 11</A
> for more explanation.</P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sect_07_02.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sect_07_04.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>More advanced if usage</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="chap_07.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Summary</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>