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

📄 ch45_22.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 2 页
字号:
34      esac35  done &lt;&quot;$template&quot; 2&gt;$errors 3&gt;&quot;$completed&quot;36  37  if [ -s $errors ]; then38      /bin/cat $errors 1&gt;&amp;239      echo &quot;$myname: should you remove '$completed' file?&quot; 1&gt;&amp;240  fi</PRE></BLOCKQUOTE></P><PCLASS="para"><BCLASS="emphasis.bold">Line&nbsp;10</B> uses the<SPANCLASS="link"><CODECLASS="literal">4&lt;&amp;0</CODE> operator (<ACLASS="linkend"HREF="ch45_21.htm"TITLE="n&gt;&amp;m: Swap Standard Output and Standard Error ">45.21</A>)</SPAN>to save the location of the original standard input&nbsp;- usually yourterminal, but not always&nbsp;- as file descriptor 4.[2](We'll need to read that original <EMCLASS="emphasis">stdin</EM> in line 24.)</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[2] We can't assume that standard input is coming from a terminal.If we do, it prevents you from running <EMCLASS="emphasis">formprog</EM> this way:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="replaceable"><I>command-generator-program</I></CODE> <CODECLASS="userinput"><B>| formprog</B></CODE>% <CODECLASS="userinput"><B>formprog &lt; </B></CODE><CODECLASS="replaceable"><I>command-file</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para"></P></BLOCKQUOTE><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-53944"></A><ACLASS="indexterm"NAME="AUTOID-53947"></A><ACLASS="indexterm"NAME="AUTOID-53950"></A><ACLASS="indexterm"NAME="AUTOID-53953"></A>During <BCLASS="emphasis.bold">lines&nbsp;11-35</B> of the redirected-I/O <EMCLASS="emphasis">while</EM> loop:all commands' standard input comes from the file named in <CODECLASS="literal">$template</CODE>,all standard error goes to the <CODECLASS="literal">$errors</CODE> file,and anything written to file descriptor 3 is added to the <CODECLASS="literal">$completed</CODE>file.UNIX keeps file pointers for all those open files&nbsp;- so each read andwrite is done just past the end of the previous one.</P><PCLASS="para">Here's what happens each time the loop is executed:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">The<SPANCLASS="link"><EMCLASS="emphasis">read</EM> command (<ACLASS="linkend"HREF="ch44_13.htm"TITLE="read: Reading from the Keyboard ">44.13</A>)</SPAN>in <BCLASS="emphasis.bold">line 11</B> reads the next line from its standard input&nbsp;- that's the open<CODECLASS="literal">$template</CODE> file.</P></LI><LICLASS="listitem"><PCLASS="para">The<SPANCLASS="link"><EMCLASS="emphasis">case</EM> (<ACLASS="linkend"HREF="ch44_05.htm"TITLE="Test String Values with Bourne Shell case ">44.5</A>)</SPAN>in lines&nbsp;15-31 checks the text from the<CODECLASS="literal">$template</CODE> file:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">If the text has both a label -ding with a colon (<CODECLASS="literal">:</CODE>)) and someother text (stored in <CODECLASS="literal">$text</CODE>), the complete line is written two places.<BCLASS="emphasis.bold">Line&nbsp;17</B> writes the line to the standard output&nbsp;- which is probablyyour screen (it's not redirected by the script, anyway).<BCLASS="emphasis.bold">Line&nbsp;18</B> writes the line to file descriptor 3, the open<CODECLASS="literal">$completed</CODE> file.</P></LI><LICLASS="listitem"><PCLASS="para">If the text has just a label, <BCLASS="emphasis.bold">line&nbsp;21</B> writes the label tostandard output (usually your terminal) without a newline.We want to read the answer, at <BCLASS="emphasis.bold">line&nbsp;24</B>, but there's a problem:on some Bourne shells, the <EMCLASS="emphasis">read</EM> command can only read from filedescriptor 0 and won't let you use operators like <CODECLASS="literal">&lt;&amp;4</CODE> on itscommand line.</P><PCLASS="para">So, in <BCLASS="emphasis.bold">line&nbsp;22</B>, we save a copy of the open <CODECLASS="literal">$template</CODE>file descriptor <EMCLASS="emphasis">and the location of the open file pointer</EM> in filedescriptor 5.<BCLASS="emphasis.bold">Line&nbsp;23</B> changes standard input so the <EMCLASS="emphasis">read</EM> in line&nbsp;24 willread from the right place (usually the terminal).<BCLASS="emphasis.bold">Line&nbsp;25</B> adjusts standard input so the next <EMCLASS="emphasis">read</EM> at the top ofthe loop (line&nbsp;11) will come from the <CODECLASS="literal">$template</CODE> file.</P><PCLASS="para">If line&nbsp;24 doesn't read an answer, <BCLASS="emphasis.bold">line&nbsp;27</B> does not write a line.Otherwise, <BCLASS="emphasis.bold">line&nbsp;28</B> writes the line to file descriptor 3, the open<CODECLASS="literal">$completed</CODE> file.</P></LI><LICLASS="listitem"><PCLASS="para">If the template label doesn't end with a colon, <BCLASS="emphasis.bold">line&nbsp;33</B> writesa message to <EMCLASS="emphasis">stderr</EM> (file descriptor 2).These messages, together with messages to <EMCLASS="emphasis">stderr</EM> from any othercommand in the loop, are redirected into the <CODECLASS="literal">$errors</CODE> file.After the loop, if the<SPANCLASS="link">test (<ACLASS="linkend"HREF="ch44_20.htm"TITLE="test: Testing Files and Strings ">44.20</A>)</SPAN>in <BCLASS="emphasis.bold">line&nbsp;37</B> sees any text in the file,the text is displayed in <BCLASS="emphasis.bold">line&nbsp;38</B> and the script prints awarning.</P></LI></UL></LI></OL><PCLASS="para">The loop keeps reading and writing line by line until the <EMCLASS="emphasis">read</EM>at the top of the loop reaches the end-of-file of <CODECLASS="literal">$template</CODE>.<ACLASS="indexterm"NAME="AUTOID-54013"></A><ACLASS="indexterm"NAME="AUTOID-54014"></A><ACLASS="indexterm"NAME="AUTOID-54015"></A></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="ch45_21.htm"TITLE="45.21 n&gt;&amp;m: Swap Standard Output and Standard Error "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.21 n&gt;&amp;m: Swap Standard Output and Standard Error "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="ch45_23.htm"TITLE="45.23 The Ins and Outs of Redirected I/O Loops "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.23 The Ins and Outs of Redirected I/O Loops "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">45.21 n&gt;&amp;m: Swap Standard Output and Standard Error </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">45.23 The Ins and Outs of Redirected I/O Loops </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 &amp; 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 + -