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

📄 nestedloops.html

📁 Shall高级编程
💻 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&nbsp;#!/bin/bash   2&nbsp;# nested-loop.sh: Nested "for" loops.   3&nbsp;   4&nbsp;outer=1             # Set outer loop counter.   5&nbsp;   6&nbsp;# Beginning of outer loop.   7&nbsp;for a in 1 2 3 4 5   8&nbsp;do   9&nbsp;  echo "Pass $outer in outer loop."  10&nbsp;  echo "---------------------"  11&nbsp;  inner=1           # Reset inner loop counter.  12&nbsp;  13&nbsp;  # ===============================================  14&nbsp;  # Beginning of inner loop.  15&nbsp;  for b in 1 2 3 4 5  16&nbsp;  do  17&nbsp;    echo "Pass $inner in inner loop."  18&nbsp;    let "inner+=1"  # Increment inner loop counter.  19&nbsp;  done  20&nbsp;  # End of inner loop.  21&nbsp;  # ===============================================  22&nbsp;  23&nbsp;  let "outer+=1"    # Increment outer loop counter.   24&nbsp;  echo              # Space between output blocks in pass of outer loop.  25&nbsp;done                 26&nbsp;# End of outer loop.  27&nbsp;  28&nbsp;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 + -