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

📄 loops.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 3 页
字号:
   7&nbsp;if [ $# -ne $EXPECTED_ARGS ]   8&nbsp;# Check for proper no. of command line args.   9&nbsp;then  10&nbsp;   echo "Usage: `basename $0` phone# text-file"  11&nbsp;   exit $E_BADARGS  12&nbsp;fi  13&nbsp;  14&nbsp;  15&nbsp;if [ ! -f "$2" ]  16&nbsp;then  17&nbsp;  echo "File $2 is not a text file"  18&nbsp;  exit $E_BADARGS  19&nbsp;fi  20&nbsp;    21&nbsp;  22&nbsp;fax make $2              # Create fax formatted files from text files.  23&nbsp;  24&nbsp;for file in $(ls $2.0*)  # Concatenate the converted files.  25&nbsp;                         # Uses wild card in variable list.  26&nbsp;do  27&nbsp;  fil="$fil $file"  28&nbsp;done    29&nbsp;  30&nbsp;efax -d /dev/ttyS3 -o1 -t "T$1" $fil   # Do the work.  31&nbsp;  32&nbsp;  33&nbsp;# As S.C. points out, the for-loop can be eliminated with  34&nbsp;#    efax -d /dev/ttyS3 -o1 -t "T$1" $2.0*  35&nbsp;# but it's not quite as instructive [grin].  36&nbsp;  37&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="WHILELOOPREF"></A><BCLASS="COMMAND">while</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 true (returns a <SPANCLASS="RETURNVALUE">0</SPAN> <AHREF="exit-status.html#EXITSTATUSREF">exit status</A>).  In contrast	      to a <AHREF="loops.html#FORLOOPREF1">for loop</A>, a	      <ICLASS="FIRSTTERM">while loop</I> finds use in situations	      where the number of loop repetitions is not known	      beforehand.</P><P><P><BCLASS="COMMAND">while</B>  [<TTCLASS="REPLACEABLE"><I>condition</I></TT>]<BR>  do <BR>  <TTCLASS="REPLACEABLE"><I>燾ommand</I></TT>... <BR>  done </P></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">while</B>  [<TTCLASS="REPLACEABLE"><I>condition</I></TT>]  ;   do </P></P><P>Note that certain specialized <ICLASS="FIRSTTERM">while	      loops</I>, as, for example,  a <AHREF="internal.html#GETOPTSX">getopts construct</A>, deviate	      somewhat from the standard template given here.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX25"></A><P><B>Example 10-14. Simple <BCLASS="COMMAND">while</B> 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;do   8&nbsp;  echo -n "$var0 "        # -n suppresses newline.   9&nbsp;  #             ^           Space, to separate printed out numbers.  10&nbsp;  11&nbsp;  var0=`expr $var0 + 1`   # var0=$(($var0+1))  also works.  12&nbsp;                          # var0=$((var0 + 1)) also works.  13&nbsp;                          # let "var0 += 1"    also works.  14&nbsp;done                      # Various other methods also work.  15&nbsp;  16&nbsp;echo  17&nbsp;  18&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX26"></A><P><B>Example 10-15. Another <BCLASS="COMMAND">while</B> 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>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. <BCLASS="COMMAND">while</B> 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>As with a <ICLASS="FIRSTTERM">for loop</I>, a	      <ICLASS="FIRSTTERM">while loop</I> may employ C-like syntax	      by using the double parentheses construct (see also <AHREF="dblparens.html#CVARS">Example 9-30</A>).</P><DIVCLASS="EXAMPLE"><HR><ANAME="WHLOOPC"></A><P><B>Example 10-17. C-like syntax in a <BCLASS="COMMAND">while</B> 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;# Now, C programmers can feel right at home in Bash.  33&nbsp;  34&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><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</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. <BCLASS="COMMAND">until</B> 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></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="dblparens.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="nestedloops.html">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">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 + -