📄 loops.html
字号:
7 if [ $# -ne $EXPECTED_ARGS ] 8 # Check for proper no. of command line args. 9 then 10 echo "Usage: `basename $0` phone# text-file" 11 exit $E_BADARGS 12 fi 13 14 15 if [ ! -f "$2" ] 16 then 17 echo "File $2 is not a text file" 18 exit $E_BADARGS 19 fi 20 21 22 fax make $2 # Create fax formatted files from text files. 23 24 for file in $(ls $2.0*) # Concatenate the converted files. 25 # Uses wild card in variable list. 26 do 27 fil="$fil $file" 28 done 29 30 efax -d /dev/ttyS3 -o1 -t "T$1" $fil # Do the work. 31 32 33 # As S.C. points out, the for-loop can be eliminated with 34 # efax -d /dev/ttyS3 -o1 -t "T$1" $2.0* 35 # but it's not quite as instructive [grin]. 36 37 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 #!/bin/bash 2 3 var0=0 4 LIMIT=10 5 6 while [ "$var0" -lt "$LIMIT" ] 7 do 8 echo -n "$var0 " # -n suppresses newline. 9 # ^ Space, to separate printed out numbers. 10 11 var0=`expr $var0 + 1` # var0=$(($var0+1)) also works. 12 # var0=$((var0 + 1)) also works. 13 # let "var0 += 1" also works. 14 done # Various other methods also work. 15 16 echo 17 18 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 #!/bin/bash 2 3 echo 4 # Equivalent to: 5 while [ "$var1" != "end" ] # while test "$var1" != "end" 6 do 7 echo "Input variable #1 (end to exit) " 8 read var1 # Not 'read $var1' (why?). 9 echo "variable #1 = $var1" # Need quotes because of "#" . . . 10 # If input is 'end', echoes it here. 11 # Does not test for termination condition until top of loop. 12 echo 13 done 14 15 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 #!/bin/bash 2 3 var1=unset 4 previous=$var1 5 6 while echo "previous-variable = $previous" 7 echo 8 previous=$var1 9 [ "$var1" != end ] # Keeps track of what $var1 was previously. 10 # Four conditions on "while", but only last one controls loop. 11 # The *last* exit status is the one that counts. 12 do 13 echo "Input variable #1 (end to exit) " 14 read var1 15 echo "variable #1 = $var1" 16 done 17 18 # Try to figure out how this all works. 19 # It's a wee bit tricky. 20 21 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 #!/bin/bash 2 # wh-loopc.sh: Count to 10 in a "while" loop. 3 4 LIMIT=10 5 a=1 6 7 while [ "$a" -le $LIMIT ] 8 do 9 echo -n "$a " 10 let "a+=1" 11 done # No surprises, so far. 12 13 echo; echo 14 15 # +=================================================================+ 16 17 # Now, repeat with C-like syntax. 18 19 ((a = 1)) # a=1 20 # Double parentheses permit space when setting a variable, as in C. 21 22 while (( a <= LIMIT )) # Double parentheses, and no "$" preceding variables. 23 do 24 echo -n "$a " 25 ((a += 1)) # let "a+=1" 26 # Yes, indeed. 27 # Double parentheses permit incrementing a variable with C-like syntax. 28 done 29 30 echo 31 32 # Now, C programmers can feel right at home in Bash. 33 34 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"><</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 #!/bin/bash 2 3 END_CONDITION=end 4 5 until [ "$var1" = "$END_CONDITION" ] 6 # Tests condition here, at top of loop. 7 do 8 echo "Input variable #1 " 9 echo "($END_CONDITION to exit)" 10 read var1 11 echo "variable #1 = $var1" 12 echo 13 done 14 15 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 + -