⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 371-374.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 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 &#147;Please choose either P, D, or Q to &#148;

        echo &#147; [P]rint a file&#148;

        echo &#147; [D]elete a file&#148;

       echo &#147; [Q]uit&#148;

# Get response from user

     read response

# Use case to match response to action

     case $response in

         P|p) echo &#147;Name of file to print?&#148;

              read filename

               lp $filename;;

         D|d) echo &#147;Name of file to delete?&#148;

             read filename

             rm $filename;;

           *) echo &#147;leaving now&#148;;;

     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);;

     &#133;

 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&#146;s &#147;case&#148; 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 &lt;P&gt;, &lt;p&gt;, &lt;D&gt;, or &lt;d&gt;, 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 &lt;Return&gt; then the names of

#      the 10 most recently modified files are displayed

# If the user types recent n &lt;Return&gt; 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 &#147;Usage: recent number-of-files&#148;;;

                esac;;

               *) echo &#147;Usage: recent number-of-files&#148;;;

      esac

</PRE>

<!-- END CODE //-->

<P><FONT SIZE="+1"><B>Finding the Exit Status</B></FONT></P>

<P>When a shell command executes, it&#146;s either successful or not. If you use the command <TT>grep &#147;American Terms&#148; 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&#146;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 &#147;American Terms&#148; customers

echo $?

</PRE>

<!-- END CODE SNIP //-->

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>NOTE:&nbsp;&nbsp;</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 &#145;who | grep $1 &gt; /dev/null` in

            0) echo &#147;$1 is logged in.&#148;;;

             *) echo &#147;$1 is not here. Try again later.&#148;;;

     esac

     echo &#147;Have a great day!&#148;

</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&#146;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&#133;then&#133;else&#133;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 (&#133;). 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&#146;t successful, the commands after the <TT>else</TT> are performed.</P>

<P>Here&#146;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 &gt; /dev/null

then

     echo &#147;$1 is logged in.&#148;

else

     echo &#147;$1 is not here. Try again later.&#148;

fi

echo &#147; Have a great day!&#148;

</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 + -