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

📄 loops.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
>	      <AHREF="loops.html#WHILENOBRACKETS">are <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN>	      mandatory</A> in a <ICLASS="FIRSTTERM">while</I> loop.	      See, for example,  the <AHREF="internal.html#GETOPTSX">getopts	      construct</A>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX25"></A><P><B>Example 10-14. Simple <ICLASS="FIRSTTERM">while</I> loop</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;var0=0   4&nbsp;LIMIT=10   5&nbsp;   6&nbsp;while [ "$var0" -lt "$LIMIT" ]   7&nbsp;#      ^                    ^   8&nbsp;# Spaces, because these are "test-brackets" . . .   9&nbsp;do  10&nbsp;  echo -n "$var0 "        # -n suppresses newline.  11&nbsp;  #             ^           Space, to separate printed out numbers.  12&nbsp;  13&nbsp;  var0=`expr $var0 + 1`   # var0=$(($var0+1))  also works.  14&nbsp;                          # var0=$((var0 + 1)) also works.  15&nbsp;                          # let "var0 += 1"    also works.  16&nbsp;done                      # Various other methods also work.  17&nbsp;  18&nbsp;echo  19&nbsp;  20&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX26"></A><P><B>Example 10-15. Another <ICLASS="FIRSTTERM">while</I> loop</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;echo   4&nbsp;                               # Equivalent to:   5&nbsp;while [ "$var1" != "end" ]     # while test "$var1" != "end"   6&nbsp;do   7&nbsp;  echo "Input variable #1 (end to exit) "   8&nbsp;  read var1                    # Not 'read $var1' (why?).   9&nbsp;  echo "variable #1 = $var1"   # Need quotes because of "#" . . .  10&nbsp;  # If input is 'end', echoes it here.  11&nbsp;  # Does not test for termination condition until top of loop.  12&nbsp;  echo  13&nbsp;done    14&nbsp;  15&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="WHMULTCOND"></A></P><P>A <ICLASS="FIRSTTERM">while loop</I> may have multiple	      conditions. Only the final condition determines when the loop	      terminates. This necessitates a slightly different loop syntax,	      however.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX26A"></A><P><B>Example 10-16. <ICLASS="FIRSTTERM">while</I> loop with multiple conditions</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;var1=unset   4&nbsp;previous=$var1   5&nbsp;   6&nbsp;while echo "previous-variable = $previous"   7&nbsp;      echo   8&nbsp;      previous=$var1   9&nbsp;      [ "$var1" != end ] # Keeps track of what $var1 was previously.  10&nbsp;      # Four conditions on "while", but only last one controls loop.  11&nbsp;      # The *last* exit status is the one that counts.  12&nbsp;do  13&nbsp;echo "Input variable #1 (end to exit) "  14&nbsp;  read var1  15&nbsp;  echo "variable #1 = $var1"  16&nbsp;done    17&nbsp;  18&nbsp;# Try to figure out how this all works.  19&nbsp;# It's a wee bit tricky.  20&nbsp;  21&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="WLOOPCSTYLE"></A></P><P>As with a <ICLASS="FIRSTTERM">for loop</I>, a	      <ICLASS="FIRSTTERM">while loop</I> may employ C-style syntax	      by using the double parentheses construct (see also <AHREF="dblparens.html#CVARS">Example 9-33</A>).</P><DIVCLASS="EXAMPLE"><HR><ANAME="WHLOOPC"></A><P><B>Example 10-17. C-style syntax in a <ICLASS="FIRSTTERM">while</I> loop</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# wh-loopc.sh: Count to 10 in a "while" loop.   3&nbsp;   4&nbsp;LIMIT=10   5&nbsp;a=1   6&nbsp;   7&nbsp;while [ "$a" -le $LIMIT ]   8&nbsp;do   9&nbsp;  echo -n "$a "  10&nbsp;  let "a+=1"  11&nbsp;done           # No surprises, so far.  12&nbsp;  13&nbsp;echo; echo  14&nbsp;  15&nbsp;# +=================================================================+  16&nbsp;  17&nbsp;# Now, repeat with C-like syntax.  18&nbsp;  19&nbsp;((a = 1))      # a=1  20&nbsp;# Double parentheses permit space when setting a variable, as in C.  21&nbsp;  22&nbsp;while (( a &#60;= LIMIT ))   # Double parentheses, and no "$" preceding variables.  23&nbsp;do  24&nbsp;  echo -n "$a "  25&nbsp;  ((a += 1))   # let "a+=1"  26&nbsp;  # Yes, indeed.  27&nbsp;  # Double parentheses permit incrementing a variable with C-like syntax.  28&nbsp;done  29&nbsp;  30&nbsp;echo  31&nbsp;  32&nbsp;# C programmers can feel right at home in Bash.  33&nbsp;  34&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="WHILEFUNC"></A></P><P>	      Inside its test brackets, a <ICLASS="FIRSTTERM">while loop</I>	      can call a <AHREF="functions.html#FUNCTIONREF">function</A>.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;t=0   2&nbsp;   3&nbsp;condition ()   4&nbsp;{   5&nbsp;  ((t++))   6&nbsp;   7&nbsp;  if [ $t -lt 5 ]   8&nbsp;  then   9&nbsp;    return 0  # true  10&nbsp;  else  11&nbsp;    return 1  # false  12&nbsp;  fi  13&nbsp;}  14&nbsp;  15&nbsp;while condition  16&nbsp;#     ^^^^^^^^^  17&nbsp;#     Function call -- four loop iterations.  18&nbsp;do  19&nbsp;  echo "Still going: t = $t"  20&nbsp;done  21&nbsp;  22&nbsp;# Still going: t = 1  23&nbsp;# Still going: t = 2  24&nbsp;# Still going: t = 3  25&nbsp;# Still going: t = 4</PRE></TD></TR></TABLE>	    	    	    	    </P><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN6086"></A><P><ANAME="WHILENOBRACKETS"></A></P><P>Similar to the <AHREF="tests.html#IFGREPREF">if-test</A>	      construct, a <ICLASS="FIRSTTERM">while</I> loop can omit the test	      brackets.	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;while condition   2&nbsp;do   3&nbsp;   ...   4&nbsp;done</PRE></TD></TR></TABLE></P></DIV></TD></TR></TABLE><P><ANAME="WHILEREADREF2"></A></P><P>By coupling the power of the <AHREF="internal.html#READREF">read</A> command with a	      <ICLASS="FIRSTTERM">while loop</I>, we get the handy <AHREF="internal.html#WHILEREADREF">while read</A> construct, useful	      for reading and parsing files.	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;cat $filename |   # Supply input from a file.   2&nbsp;while read line   # As long as there is another line to read ...   3&nbsp;do   4&nbsp;  ...   5&nbsp;done</PRE></TD></TR></TABLE></P><P><ANAME="WHREDIR"></A></P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>A <ICLASS="FIRSTTERM">while loop</I> may have its	      <TTCLASS="FILENAME">stdin</TT> <AHREF="redircb.html#REDIRREF">redirected to a file</A> by a	      <SPANCLASS="TOKEN">&#60;</SPAN> at its end.</P><P>A <ICLASS="FIRSTTERM">while loop</I> may have its	      <TTCLASS="FILENAME">stdin</TT> <AHREF="internal.html#READPIPEREF">	      supplied by a pipe</A>.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="UNTILLOOPREF"></A><BCLASS="COMMAND">until</B></DT><DD><P>This construct tests for a condition at the top of a loop, and keeps	      looping as long as that condition is false (opposite of	      <ICLASS="FIRSTTERM">while loop</I>).</P><P><P><BCLASS="COMMAND">until</B>  [<TTCLASS="REPLACEABLE"><I> condition-is-true </I></TT>]<BR>  do <BR>  <TTCLASS="REPLACEABLE"><I>燾ommand(s)</I></TT>... <BR>  done </P></P><P>Note that an <ICLASS="FIRSTTERM">until loop</I> tests for the	      terminating condition at the top of the loop, differing from a	      similar construct in some programming languages.</P><P>As is the case with <ICLASS="FIRSTTERM">for loops</I>,	      placing the <ICLASS="FIRSTTERM">do</I> on the same line as	      the condition test requires a semicolon.</P><P><P><BCLASS="COMMAND">until</B>  [<TTCLASS="REPLACEABLE"><I> condition-is-true </I></TT>]  ;   do </P></P><DIVCLASS="EXAMPLE"><HR><ANAME="EX27"></A><P><B>Example 10-18. <ICLASS="FIRSTTERM">until</I> loop</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;END_CONDITION=end   4&nbsp;   5&nbsp;until [ "$var1" = "$END_CONDITION" ]   6&nbsp;# Tests condition here, at top of loop.   7&nbsp;do   8&nbsp;  echo "Input variable #1 "   9&nbsp;  echo "($END_CONDITION to exit)"  10&nbsp;  read var1  11&nbsp;  echo "variable #1 = $var1"  12&nbsp;  echo  13&nbsp;done    14&nbsp;  15&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD></DL></DIV><P><ANAME="CHOOSELOOP"></A></P><P>How to choose between a <ICLASS="FIRSTTERM">for</I> loop or a	  <ICLASS="FIRSTTERM">while</I> loop or	  <ICLASS="FIRSTTERM">until</I> loop? In <BCLASS="COMMAND">C</B>,	  you would typically use a <ICLASS="FIRSTTERM">for</I> loop	  when the number of loop iterations is known beforehand. With	  <ICLASS="FIRSTTERM">Bash</I>, however, the situation is	  fuzzier. The Bash <ICLASS="FIRSTTERM">for</I> loop is more	  loosely structured and more flexible than its equivalent in	  other languages. Therefore, feel free to use whatever type	  of loop gets the job done in the simplest way.</P></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN5815"HREF="loops.html#AEN5815">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P><ANAME="ITERATIONREF"></A><ICLASS="FIRSTTERM">Iteration</I>:	  Repeated execution of a command or group of commands --	  usually, but not always -- while a given condition holds,	  or until a given condition is met.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="dblparens.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="nestedloops.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">The Double Parentheses Construct</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part3.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Nested Loops</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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