📄 ch09_12.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 9] 9.12 The Bourne Shell for Loop </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:35:05Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch09_01.htm"TITLE="9. Saving Time on the Command Line"><LINKREL="prev"HREF="ch09_11.htm"TITLE="9.11 Repeating a Command with a foreach Loop "><LINKREL="next"HREF="ch09_13.htm"TITLE="9.13 Multiline Commands, Secondary Prompts "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch09_11.htm"TITLE="9.11 Repeating a Command with a foreach Loop "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.11 Repeating a Command with a foreach Loop "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 9<BR>Saving Time on the Command Line</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch09_13.htm"TITLE="9.13 Multiline Commands, Secondary Prompts "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.13 Multiline Commands, Secondary Prompts "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7690">9.12 The Bourne Shell for Loop </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-10386"></A><ACLASS="indexterm"NAME="AUTOID-10389"></A><ACLASS="indexterm"NAME="AUTOID-10391"></A>The Bourne shell <EMCLASS="emphasis">for</EM> loop is like the C shell<SPANCLASS="link"><EMCLASS="emphasis">foreach</EM> loop (<ACLASS="linkend"HREF="ch09_11.htm"TITLE="Repeating a Command with a foreach Loop ">9.11</A>)</SPAN>:it loops through a list of words, running one or more commands for eachword in the list.This saves time when you want to run the same series of commands,separately, on several files.</P><PCLASS="para">Let's use the example from the article about <EMCLASS="emphasis">foreach</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>for file in /usr/fran/report /usr/rob/file2 /usr/rob/file3</B></CODE>> <CODECLASS="userinput"><B>do</B></CODE>> <CODECLASS="userinput"><B>cat -t -v $file | pg</B></CODE>> <CODECLASS="userinput"><B>done</B></CODE> ...<EMCLASS="emphasis">Shell runs cat -t -v /usr/fran/report | pg</EM>... ...<EMCLASS="emphasis">Shell runs cat -t -v /usr/rob/file2 | pg</EM>... ...<EMCLASS="emphasis">Shell runs cat -t -v /usr/rob/file3 | pg</EM>...$</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-10410"></A><ACLASS="indexterm"NAME="AUTOID-10413"></A>The right angle brackets (<CODECLASS="literal">></CODE>) are<SPANCLASS="link">secondary prompts (<ACLASS="linkend"HREF="ch09_13.htm"TITLE="Multiline Commands, Secondary Prompts ">9.13</A>)</SPAN>;the Bourne shell will keep printing them until you type the command <EMCLASS="emphasis">done</EM>.Then it runs the loop.You don't have to press RETURN after the <CODECLASS="literal">do</CODE>; you can type thefirst command on the same line after it.</P><PCLASS="para">In a shell script, the loop body (the lines between <CODECLASS="literal">do</CODE> and<CODECLASS="literal">done</CODE>) are usually indented for clarity.</P><PCLASS="para">The list after the <CODECLASS="literal">in</CODE> doesn't have to be filenames.Among other things, you can use<SPANCLASS="link">backquotes (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN>(command substitution),<SPANCLASS="link">variables (<ACLASS="linkend"HREF="ch06_08.htm"TITLE="Shell Variables ">6.8</A>, <ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>)</SPAN>,or<SPANCLASS="link">wildcards (<ACLASS="linkend"HREF="ch15_01.htm#UPT-ART-1480"TITLE="File Naming Wildcards ">15.1</A>)</SPAN>.For example, you could have typed the above loop this way:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>for file in /usr/fran/report /usr/rob/file[23]</B></CODE>> <CODECLASS="userinput"><B>do cat -t -v $file | pg</B></CODE>> <CODECLASS="userinput"><B>done</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If you want the loop to stop before or after running each command, add theBourne shell<ACLASS="indexterm"NAME="AUTOID-10434"></A><SPANCLASS="link"><EMCLASS="emphasis">read</EM> command (<ACLASS="linkend"HREF="ch44_13.htm"TITLE="read: Reading from the Keyboard ">44.13</A>)</SPAN>.It reads keyboard input and waits for a RETURN.In this case, you can probably ignore the input; you'll use <EMCLASS="emphasis">read</EM> tomake the loop wait.For example, to make the above loop prompt before each command line:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>for file in /usr/fran/report /usr/rob/file[23]</B></CODE>> <CODECLASS="userinput"><B>do</B></CODE>> <CODECLASS="userinput"><B>echo -n "Press RETURN to see $file-"</B></CODE>> <CODECLASS="userinput"><B>read x</B></CODE>> <CODECLASS="userinput"><B>cat -t -v $file | pg</B></CODE>> <CODECLASS="userinput"><B>done</B></CODE>Press RETURN to see /usr/fran/report-<CODECLASS="userinput"><B><KBDCLASS="keycap">[RETURN]</KBD></B></CODE> <EMCLASS="emphasis">Shell runs cat -t -v /usr/fran/report | pg</EM>...Press RETURN to see /usr/rob/file2-<CODECLASS="userinput"><B><KBDCLASS="keycap">[RETURN]</KBD></B></CODE> <EMCLASS="emphasis">Shell runs cat -t -v /usr/rob/file2 | pg</EM>...Press RETURN to see /usr/rob/file3-<CODECLASS="userinput"><B><KBDCLASS="keycap">[RETURN]</KBD></B></CODE> <EMCLASS="emphasis">Shell runs cat -t -v /usr/rob/file3 | pg</EM>...</PRE></BLOCKQUOTE></P><PCLASS="para"> Article<ACLASS="xref"HREF="ch44_16.htm"TITLE="Handling Command-Line Arguments with a for Loop ">44.16</A>has more information about the <EMCLASS="emphasis">for</EM> loop.Article<ACLASS="xref"HREF="ch45_16.htm"TITLE="Standard Input to a for Loop ">45.16</A>shows how to make a <EMCLASS="emphasis">for</EM> loop read the standard input instead of alist of arguments.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch09_11.htm"TITLE="9.11 Repeating a Command with a foreach Loop "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.11 Repeating a Command with a foreach Loop "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch09_13.htm"TITLE="9.13 Multiline Commands, Secondary Prompts "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.13 Multiline Commands, Secondary Prompts "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">9.11 Repeating a Command with a foreach Loop </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">9.13 Multiline Commands, Secondary Prompts </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -