272-275.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 189 行

HTML
189
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Shell Programming</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=14//-->

<!--PAGES=272-275//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="270-272.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="276-278.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">The if Statement</FONT></H4>

<P>All three shells support nested <TT>if-then-else</TT> statements. These statements provide you with a way of performing complicated conditional tests in your shell programs. The syntax of the <TT>if</TT> statement is the same for <TT>bash</TT> and <TT>pdksh</TT> and is shown here:</P>

<!-- CODE //-->

<PRE>

if [ <I>expression</I> ]

then

      <I>commands</I>

elif [ <I>expression2</I> ]

then

      <I>commands</I>

else

      <I>commands</I>

fi

</PRE>

<!-- END CODE //-->

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>The <TT>elif</TT> and <TT>else</TT> clauses are both optional parts of the <TT>if</TT> statement. Also note that <TT>bash</TT> and <TT>pdksh</TT> use the reverse of the statement name in most of their complex statements to signal the end of the statement. In this statement, the <TT>fi</TT> keyword is used to signal the end of the <TT>if</TT> statement.<HR></FONT>

</BLOCKQUOTE>

<P>The <TT>elif</TT> statement is an abbreviation of <TT>else if</TT>. This statement is executed only if none of the expressions associated with the <TT>if</TT> statement or any <TT>elif</TT> statements before it were true. The commands associated with the <TT>else</TT> statement are executed only if none of the expressions associated with the <TT>if</TT> statement or any of the <TT>elif</TT> statements were true.</P>

<P>In <TT>tcsh</TT>, the <TT>if</TT> statement has two different forms. The first form provides the same function as the <TT>bash</TT> and <TT>pdksh if</TT> statement. This form of <TT>if</TT> statement has the following syntax:</P>

<!-- CODE SNIP //-->

<PRE>

if (<I>expression1</I>) then

      <I>commands</I>

else if (<I>expression2</I>) then

      <I>commands</I>

else

      <I>commands</I>

endif

</PRE>

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

<P>The second form of <TT>if</TT> statement provided by <TT>tcsh</TT> is a simple version of the first <TT>if</TT> statement. This form of <TT>if</TT> statement evaluates only a single expression. If the expression is true, it executes a single command; if the expression is false, nothing happens. The syntax for this form of <TT>if</TT> statement is the following:</P>

<!-- CODE SNIP //-->

<PRE>

if (<I>expression</I>) <I>command</I>

</PRE>

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

<P>This statement can be written using the first form of <TT>if</TT> statement by writing the <TT>if</TT> without any <TT>else</TT> or <TT>else if</TT> clauses. This form just saves a little typing.</P>

<P>The following is an example of a <TT>bash</TT> or <TT>pdksh if</TT> statement. This statement checks to see whether there is a <TT>.profile</TT> file in the current directory:</P>

<!-- CODE SNIP //-->

<PRE>

if [ -f .profile ]

then

      echo &#147;There is a .profile file in the current directory.&#148;

else

      echo &#147;Could not find the .profile file.&#148;

fi

</PRE>

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

<P>The same statement written using the <TT>tcsh</TT> syntax is shown here:</P>

<!-- CODE SNIP //-->

<PRE>

#

if ( &#123; -f .profile &#125; ) then

     echo &#147;There is a .profile file in the current directory.&#148;

else

     echo &#147;Could not find the .profile file.&#148;

endif

</PRE>

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

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>Notice that in the <TT>tcsh</TT> example the first line starts with a <TT>#</TT>. This is required for <TT>tcsh</TT> to recognize the file containing the commands as a <TT>tcsh</TT> script file.<HR></FONT>

</BLOCKQUOTE>

<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">The case Statement</FONT></H4>

<P>The <TT>case</TT> statement enables you to compare a pattern with several other patterns and execute a block of code if a match is found. The shell <TT>case</TT> statement is quite a bit more powerful than the <TT>case</TT> statement in Pascal or the <TT>switch</TT> statement in C. This is because in the shell <TT>case</TT> statement you can compare strings with wildcard characters in them, whereas with the Pascal and C equivalents, you can only compare enumerated types or integer values.</P>

<P>Once again, the syntax for the <TT>case</TT> statement is identical for <TT>bash</TT> and <TT>pdksh</TT> and different for <TT>tcsh</TT>. The syntax for <TT>bash</TT> and <TT>pdksh</TT> is the following:</P>

<!-- CODE SNIP //-->

<PRE>

case string1 in

      str1)

            <I>commands</I>;;

      str2)

            <I>commands</I>;;

      *)

            <I>commands</I>;;

esac

</PRE>

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

<P><TT>string1</TT> is compared to <TT>str1</TT> and <TT>str2</TT>. If one of these strings matches <TT>string1</TT>, all commands up to the double semicolon (<TT>;;</TT>) are executed. If neither <TT>str1</TT> nor <TT>str2</TT> matches <TT>string1</TT>, the commands associated with the asterisk are executed. This is the default case condition because the asterisk matches all strings.</P>

<P>The <TT>tcsh</TT> equivalent of the <TT>bash</TT> and <TT>pdksh case</TT> statement is called the <TT>switch</TT> statement. This statement&#146;s syntax closely follows the C <TT>switch</TT> statement syntax. Here it is:</P>

<!-- CODE //-->

<PRE>

switch (string1)

      case str1:

           <I>statements</I>

      breaksw

      case str2:

           <I>statements</I>

      breaksw

      default:

           <I>statements</I>

      breaksw

endsw

</PRE>

<!-- END CODE //-->

<P>This behaves in the same manner as the <TT>bash</TT> and <TT>pdksh case</TT> statement. Each string following the keyword <TT>case</TT> is compared with <TT>string1</TT>. If any of these strings matches <TT>string1</TT>, the code follows it until the <TT>breaksw</TT> keyword is executed. If none of the strings match, the code follows the default keyword until the <TT>breaksw</TT> keyword is executed.</P>

<P>The following code is an example of a <TT>bash</TT> or <TT>pdksh case</TT> statement. This code checks to see if the first command-line option is <TT>-i</TT> or <TT>-e</TT>. If it is <TT>-i</TT>, the program counts the number of lines in the file specified by the second command-line option that begins with the letter <TT>i</TT>. If the first option is <TT>-e</TT>, the program counts the number of lines in the file specified by the second command-line option that begins with the letter e. If the first command-line option is not <TT>-i</TT> or <TT>-e</TT>, the program prints a brief error message to the screen.</P>

<!-- CODE //-->

<PRE>

case &#36;1 in

   -i)

      count=&#145;grep ^i &#36;2 | wc -l&#145;

      echo &#147;The number of lines in &#36;2 that start with an i is &#36;count&#148;

      ;;

   -e)

      count=&#145;grep ^e &#36;2 | wc -l&#145;

      echo &#147;The number of lines in &#36;2 that start with an e is &#36;count&#148;

      ;;

   * )

      echo &#147;That option is not recognized&#148;

      ;;

esac

</PRE>

<!-- END CODE //-->

<P>The same example written in <TT>tcsh</TT> syntax is shown here:</P>

<!-- CODE //-->

<PRE>

# remember that the first line must start with a # when using tcsh

switch ( &#36;1 )

   case -i | i:

      set count = &#145;grep ^i &#36;2 | wc -l&#145;

      echo &#147;The number of lines in &#36;2 that begin with i is &#36;count&#148;

   breaksw

   case -e | e:

      set count = &#145;grep ^e &#36;2 | wc -l&#145;

      echo &#147;The number of lines in &#36;2 that begin with e is &#36;count&#148;

   breaksw

   default:

      echo &#147;That option is not recognized&#148;

   breaksw

endsw

</PRE>

<!-- END CODE //-->

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="270-272.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="276-278.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?