276-278.html

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

HTML
181
字号
<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=276-278//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

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

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

<TD><A HREF="279-281.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H3><A NAME="Heading13"></A><FONT COLOR="#000077">Iteration Statements</FONT></H3>

<P>The shell languages also provide several iteration or looping statements. The most commonly used of these is the <TT>for</TT> statement. In addition to the <TT>for</TT> loop, there are several others (such as <TT>while</TT> and <TT>until</TT>) but they are all variations of the same approach. The <TT>for</TT>loop is by far the most commonly used in shell programs.</P>

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

<P>The <TT>for</TT> statement executes the commands that are contained within it a specified number of times. <TT>bash</TT> and <TT>pdksh</TT> have two variations of the <TT>for</TT> statement. The <TT>for</TT> statement syntax is the same in both <TT>bash</TT> and <TT>pdksh</TT>.</P>

<P>The first form of <TT>for</TT> statement that <TT>bash</TT> and <TT>pdksh</TT> support has the following syntax:</P>

<!-- CODE SNIP //-->

<PRE>

for var1 in list

do

      <I>commands</I>

done

</PRE>

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

<P>In this form, the <TT>for</TT> statement executes once for each item in the list. This list can be a variable that contains several words separated by spaces or it can be a list of values that is typed directly into the statement. Each time through the loop, the variable <TT>var1</TT> is assigned to the current item in the list until the last one is reached.</P>

<P>The second form of <TT>for</TT> statement has the following syntax:</P>

<!-- CODE SNIP //-->

<PRE>

for var1

do

      <I>statements</I>

done

</PRE>

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

<P>In this form, the <TT>for</TT> statement executes once for each item in the variable <TT>var1</TT>. When this syntax of the <TT>for</TT> statement is used, the shell program assumes that the <TT>var1</TT> variable contains all the positional parameters that were passed into the shell program on the command line.</P>

<P>Typically, this form of <TT>for</TT> statement is the equivalent of writing the following <TT>for</TT> statement:</P>

<!-- CODE SNIP //-->

<PRE>

for var1 in &#147;&#36;&#64;&#148;

do

      <I>statements</I>

done

</PRE>

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

<P>The equivalent of the <TT>for</TT> statement in <TT>tcsh</TT> is called the <TT>foreach</TT> statement. It behaves in the same manner as the <TT>bash</TT> and <TT>pdksh for</TT> statement. The syntax of the <TT>foreach</TT> statement is the following:</P>

<!-- CODE SNIP //-->

<PRE>

foreach name (<I>list</I>)

      <I>commands</I>

end

</PRE>

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

<P>The following is an example of the <TT>bash</TT> or <TT>pdksh</TT> style of <TT>for</TT> statement. This example takes as command-line options any number of text files. The program reads in each of these files, converts all the letters to uppercase, and then stores the results in a file of the same name but with a <TT>.caps</TT> extension.</P>

<!-- CODE SNIP //-->

<PRE>

for file

do

tr a-z A-Z &lt; &#36;file &gt;&#36;file.caps

done

</PRE>

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

<P>The same example written in <TT>tcsh</TT> shell language is shown next:</P>

<!-- CODE SNIP //-->

<PRE>

#

foreach file (&#36;*)

   tr a-z A-Z &lt; &#36;file &gt;&#36;file.caps

end

</PRE>

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

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

<P>Another iteration statement offered by the shell programming language is the <TT>while</TT> statement. This statement causes a block of code to be executed while a provided conditional expression is true. The syntax for the <TT>while</TT> statement in <TT>bash</TT> and <TT>pdksh</TT> is the following:</P>

<!-- CODE SNIP //-->

<PRE>

while <I>expression</I>

do

      <I>statements</I>

done

</PRE>

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

<P>The syntax for the <TT>while</TT> statement in <TT>tcsh</TT> is the following:</P>

<!-- CODE SNIP //-->

<PRE>

while (<I>expression</I>)

      <I>statements</I>

end

</PRE>

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

<P>The following is an example of the <TT>bash</TT> and <TT>pdksh</TT> style of <TT>while</TT> statement. This program lists the parameters that were passed to the program, along with the parameter number.</P>

<!-- CODE SNIP //-->

<PRE>

count=1

while [ -n &#147;&#36;*&#148; ]

do

      echo &#147;This is parameter number &#36;count &#36;1&#148;

      shift

      count=&#145;expr &#36;count &#43; 1&#145;

done

</PRE>

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

<P>As you will see in the section titled &#147;The <TT>shift</TT> Command,&#148; the <TT>shift</TT> command moves the command-line parameters over one space to the left.</P>

<P>The same program written in the <TT>tcsh</TT> language is shown next:</P>

<!-- CODE SNIP //-->

<PRE>

#

set count = 1

while ( &#147;&#36;*&#148; != &#147;&#148; )

      echo &#147;This is parameter number &#36;count &#36;1&#148;

      shift

      set count = &#145;expr &#36;count &#43; 1&#145;

end

</PRE>

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

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

<P>The <TT>until</TT> statement is very similar in syntax and function to the <TT>while</TT> statement. The only real difference between the two is that the <TT>until</TT> statement executes its code block while its conditional expression is false, and the <TT>while</TT> statement executes its code block while its conditional expression is true. The syntax for the <TT>until</TT> statement in <TT>bash</TT> and <TT>pdksh</TT> is</P>

<!-- CODE SNIP //-->

<PRE>

until <I>expression</I>

do

      <I>commands</I>

done

</PRE>

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

<P>The same example that was used for the <TT>while</TT> statement can be used for the <TT>until</TT> statement. All you have to do to make it work is negate the condition. This is shown in the following code:</P>

<!-- CODE SNIP //-->

<PRE>

count=1

until [ -z &#147;&#36;*&#148; ]

do

      echo &#147;This is parameter number &#36;count &#36;1&#148;

      shift

      count=&#145;expr &#36;count &#43; 1&#145;

done

</PRE>

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

<P>The only difference between this example and the <TT>while</TT> statement example is that the <TT>-n</TT> test command option (which means that the string has nonzero length) was removed, and the <TT>-z</TT> test option (which means that the string has zero length) was put in its place.</P>

<P>In practice, the <TT>until</TT> statement is not very useful because any <TT>until</TT> statement you write can also be written as a <TT>while</TT> statement. <TT>tcsh</TT> does not have an equivalent of the <TT>until</TT> statement other than rewriting it as a <TT>while</TT> loop.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

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

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

<TD><A HREF="279-281.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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