📄 371-374.html
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Linux, Fourth Edition:Understanding Linux Shells</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0789717468//-->
<!--TITLE=Special Edition Using Linux, Fourth Edition//-->
<!--AUTHOR=Jack Tackett//-->
<!--AUTHOR=Jr.//-->
<!--AUTHOR=Steve Burnett//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=18//-->
<!--PAGES=371-374//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="369-371.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="374-376.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><FONT SIZE="+1"><B>Using case</B></FONT></P>
<P>The <TT>case</TT> structure is a decision structure that lets you select one of several courses of action, based on the value of a variable. Listing 18.2 shows a short menu program.</P>
<P><B>Listing 18.2</B> Implementing a Menu Shell Script with case</P>
<!-- CODE //-->
<PRE>
# Name: ShrtMenu
# Purpose: Allow user to print a file, delete a file,
# or quit the program
# Display menu
echo “Please choose either P, D, or Q to ”
echo “ [P]rint a file”
echo “ [D]elete a file”
echo “ [Q]uit”
# Get response from user
read response
# Use case to match response to action
case $response in
P|p) echo “Name of file to print?”
read filename
lp $filename;;
D|d) echo “Name of file to delete?”
read filename
rm $filename;;
*) echo “leaving now”;;
esac
</PRE>
<!-- END CODE //-->
<P>The syntax of the <TT>case</TT> statement is this:</P>
<!-- CODE SNIP //-->
<PRE>
case word in
pattern) statement(s);;
pattern) statement(s);;
…
esac
</PRE>
<!-- END CODE SNIP //-->
<P>The <BIG>word</BIG> parameter is matched against each <BIG>pattern</BIG> parameter, starting with the pattern at the top of the list. The statements that execute if <BIG>word</BIG> matches a pattern are terminated by two semicolons (;;). The end of the <TT>case</TT> statement is marked by the word <TT>esac</TT> (that’s “case” spelled backward).</P>
<P>In Listing 18.2, the pipe character was used to give a choice for a match. For example, <TT>P|p</TT> means that either an uppercase or lowercase letter P is considered a match.</P>
<P>The pattern <TT>*</TT> is used to represent all other patterns not explicitly stated. If users press any key besides <P>, <p>, <D>, or <d>, they exit from the menu.</P>
<P>Listing 18.3 uses a <TT>case</TT> statement that makes a selection based on the number of parameters the shell represents as <TT>$#</TT>.</P>
<P><B>Listing 18.3</B> Command-Line Parsing with case</P>
<!-- CODE //-->
<PRE>
# Name: recent
# Purpose: list the most recent files in a directory
# If user types recent <Return> then the names of
# the 10 most recently modified files are displayed
# If the user types recent n <Return> then the names of
# the n most recently modified files are displayed
# Otherwise, user is notified of incorrect usage
#
# Case based on number of parameters
case $# in
0) ls -lt | head ;;
# ls -lt lists names of file in order of
# most recently modified
# head displays the first 10 lines of a file
1) case $1 in
[0-9]*) ls -lt | head -$1 ;;
*)echo “Usage: recent number-of-files”;;
esac;;
*) echo “Usage: recent number-of-files”;;
esac
</PRE>
<!-- END CODE //-->
<P><FONT SIZE="+1"><B>Finding the Exit Status</B></FONT></P>
<P>When a shell command executes, it’s either successful or not. If you use the command <TT>grep “American Terms” customers</TT> to see whether the string <BIG>American Terms</BIG> is in the file customers, and the file exits, you have read permission to the file, and <BIG>American Terms</BIG> is in the file, the shell command has executed successfully. If any of those conditions isn’t true, the shell command executes unsuccessfully.</P>
<P>The shell always reports back about the status of the termination of a command, program, or shell script. The value reported back is called the <BIG>exit status</BIG> of a command and is represented by the variable <TT>#?</TT>. If you enter the following commands, you see the value of <TT>$?</TT>.</P>
<!-- CODE SNIP //-->
<PRE>
grep “American Terms” customers
echo $?
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>If <TT>$?</TT> has a value of 0, this command was successful; otherwise, the command was unsuccessful.<HR></FONT>
</BLOCKQUOTE>
<P>The following is an example in which the exit status of the command <TT>who|grep $1</TT> is used in the <TT>case</TT> statement:</P>
<!-- CODE //-->
<PRE>
# Name: just.checking
# Purpose: Determine if person is logged in
# Usage: just.checking login_name
#
case ‘who | grep $1 > /dev/null` in
0) echo “$1 is logged in.”;;
*) echo “$1 is not here. Try again later.”;;
esac
echo “Have a great day!”
</PRE>
<!-- END CODE //-->
<P>If you enter <TT>just.checking rflame</TT> and rflame is logged in, you see the following:</P>
<!-- CODE SNIP //-->
<PRE>
rflame is logged in.
Have a great day!
</PRE>
<!-- END CODE SNIP //-->
<P>If rflame isn’t logged in, you see this instead:
</P>
<!-- CODE SNIP //-->
<PRE>
rflame is not here. Try again later.
Have a great day!
</PRE>
<!-- END CODE SNIP //-->
<P><FONT SIZE="+1"><B>Using if Structures</B></FONT></P>
<P>The <TT>if…then…else…fi</TT> structure is a decision structure that allows you to select one of two courses of action based on the result of a command. The <TT>else</TT> portion of the structure is optional. One or more commands go in place of the ellipsis (…). Provided that the exit status of the last command following the <TT>if</TT> is zero (that is, the command executed successfully), the commands following the <TT>then</TT> and preceding the <TT>else</TT> (if there is one) are executed. Otherwise, the commands following the <TT>else</TT> are executed.</P>
<P>In other words, one or more commands are executed. If the last command was successful, the commands in the <TT>then</TT> portion of the statement are performed and then the commands following the <TT>fi</TT> (the end of the structure) are executed. If the last commands aren’t successful, the commands after the <TT>else</TT> are performed.</P>
<P>Here’s a familiar example that behaves exactly the same as when it was written using the <TT>case</TT> statement:</P>
<!-- CODE //-->
<PRE>
# Name: just.checking
# Purpose: Determine if person is logged in
# Usage: just.checking login_name
#
if
who | grep $1 > /dev/null
then
echo “$1 is logged in.”
else
echo “$1 is not here. Try again later.”
fi
echo “ Have a great day!”
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="369-371.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="374-376.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -