📄 nestedloops.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Nested Loops</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Loops and Branches"HREF="loops.html"><LINKREL="PREVIOUS"TITLE="Loops and Branches"HREF="loops.html"><LINKREL="NEXT"TITLE="Loop Control"HREF="loopcontrol.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="loops.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 10. Loops and Branches</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="loopcontrol.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="NESTEDLOOPS"></A>10.2. Nested Loops</H1><P>A <ICLASS="FIRSTTERM">nested loop</I> is a loop within a loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes. Of course, a <ICLASS="FIRSTTERM">break</I> within either the inner or outer loop would interrupt this process.</P><DIVCLASS="EXAMPLE"><HR><ANAME="NESTEDLOOP"></A><P><B>Example 10-19. Nested Loop</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # nested-loop.sh: Nested "for" loops. 3 4 outer=1 # Set outer loop counter. 5 6 # Beginning of outer loop. 7 for a in 1 2 3 4 5 8 do 9 echo "Pass $outer in outer loop." 10 echo "---------------------" 11 inner=1 # Reset inner loop counter. 12 13 # =============================================== 14 # Beginning of inner loop. 15 for b in 1 2 3 4 5 16 do 17 echo "Pass $inner in inner loop." 18 let "inner+=1" # Increment inner loop counter. 19 done 20 # End of inner loop. 21 # =============================================== 22 23 let "outer+=1" # Increment outer loop counter. 24 echo # Space between output blocks in pass of outer loop. 25 done 26 # End of outer loop. 27 28 exit 0</PRE></TD></TR></TABLE><HR></DIV><P>See <AHREF="arrays.html#BUBBLE">Example 26-11</A> for an illustration of nested <AHREF="loops.html#WHILELOOPREF">while loops</A>, and <AHREF="arrays.html#EX68">Example 26-13</A> to see a while loop nested inside an <AHREF="loops.html#UNTILLOOPREF">until loop</A>.</P></DIV><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="loops.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="loopcontrol.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Loops and Branches</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="loops.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Loop Control</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -