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

📄 ch09_18.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 9] 9.18 Process Substitution </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:35:17Z"><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_17.htm"TITLE="9.17 Handling Lots of Text with Temporary Files "><LINKREL="next"HREF="ch09_19.htm"TITLE="9.19 For the Impatient: Type-Ahead "></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_17.htm"TITLE="9.17 Handling Lots of Text with Temporary Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.17 Handling Lots of Text with Temporary Files "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_19.htm"TITLE="9.19 For the Impatient: Type-Ahead "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.19 For the Impatient: Type-Ahead "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0109">9.18 Process Substitution </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-109-IX-PROCESS-SUBSTITUTION"></A>Do you find yourself making temporary files, then giving those files to somecommands to read?For example, maybe you want to compare two files with<SPANCLASS="link"><EMCLASS="emphasis">comm</EM> (<ACLASS="linkend"HREF="ch28_12.htm"TITLE="Comparing Two Files with comm ">28.12</A>)</SPAN>-but <EMCLASS="emphasis">comm</EM>needs sorted files, and these files aren't sorted.So you have to type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">bash$ <CODECLASS="userinput"><B>sort file1 &gt; /tmp/file1.sort</B></CODE>bash$ <CODECLASS="userinput"><B>sort file2 &gt; /tmp/file2.sort</B></CODE>bash$ <CODECLASS="userinput"><B>comm /tmp/file1.sort /tmp/file2.sort</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">There are easier ways to do that.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-109-SECT-1.1">9.18.1 bash Process Substitution </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-10740"></A><EMCLASS="emphasis">bash</EM> has the operator <CODECLASS="literal">&lt;(</CODE><CODECLASS="replaceable"><I>process</I></CODE><CODECLASS="literal">)</CODE>.It runs a <CODECLASS="replaceable"><I>process</I></CODE> and gives the output to a named pipe.Then the filename of the named pipe becomes a command-lineargument.Here's an example that shows two unsorted files and the result:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">bash$ <CODECLASS="userinput"><B>cat file1</B></CODE>rcsdiff.logrunsedrunsed.newecho.wherefoobash$ <CODECLASS="userinput"><B>cat file2</B></CODE>newprogramrunsedecho.wherefoobash$ <CODECLASS="userinput"><B>comm &lt;(sort file1) &lt;(sort file2)</B></CODE>                echo.where                foo        newprogramrcsdiff.log                runsedrunsed.new</PRE></BLOCKQUOTE></P><PCLASS="para">(In the first column, <EMCLASS="emphasis">comm</EM> shows lines only in <EMCLASS="emphasis">file1</EM>.The second column shows lines only in <EMCLASS="emphasis">file2</EM>.The third column shows lines that were in both files.)</P><PCLASS="para">Let's take a closer look at how that works.By setting the<SPANCLASS="link"><EMCLASS="emphasis">-x</EM> option (<ACLASS="linkend"HREF="ch08_17.htm"TITLE="verbose and echo Variables Show Quoting ">8.17</A>)</SPAN>,the shellwill display the processes it runs with a <CODECLASS="literal">+</CODE> before each top-levelprocess and <CODECLASS="literal">++</CODE> before second-level processes:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">bash$ <CODECLASS="userinput"><B>set -x</B></CODE>bash$ <CODECLASS="userinput"><B>comm &lt;(sort file1) &lt;(sort file2)</B></CODE>+ comm /tmp/sh-np-a11167 /tmp/sh-np-b11167++ sort file1++ sort file2                echo.where                foo        newprogramrcsdiff.log                runsedrunsed.new</PRE></BLOCKQUOTE></P><PCLASS="para">The script made its named pipes in <EMCLASS="emphasis">/tmp</EM>.<EMCLASS="emphasis">bash</EM> ran each <EMCLASS="emphasis">sort</EM> command, sent its output to a named pipe,and put the pipe's filename on the command line.When the <EMCLASS="emphasis">comm</EM> program finished, the named pipes were deleted.</P><PCLASS="para">I've run into problems with this operator in some cases:when the process reading from a named pipe &quot;hung&quot; and never made anyoutput.For example, that happened when I replaced <EMCLASS="emphasis">comm</EM> with <EMCLASS="emphasis">diff</EM>:I'd get no output from <EMCLASS="emphasis">diff</EM>.I worked around the problem byclosing the standard output of each process with the<SPANCLASS="link"><CODECLASS="literal">&gt;&amp;-</CODE> operator (<ACLASS="linkend"HREF="ch45_21.htm"TITLE="n&gt;&amp;m: Swap Standard Output and Standard Error ">45.21</A>)</SPAN>,like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">bash$ <CODECLASS="userinput"><B>diff &lt;(sort file1; exec &gt;&amp;-) &lt;(sort file2; exec &gt;&amp;-)</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">That made <EMCLASS="emphasis">diff</EM> happy; it showed me the differences between thetwo sorted files.</P><PCLASS="para"><EMCLASS="emphasis">bash</EM> also has a similar operator, <CODECLASS="literal">&gt;(&nbsp;)</CODE>, which takes theinput of a process from a named pipe.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-109-SECT-1.2">9.18.2 Automatic Temporary Files with ! </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-10788"></A><ACLASS="indexterm"NAME="AUTOID-10791"></A>If you don't have <EMCLASS="emphasis">bash</EM>, you can use the shell script named <CODECLASS="replaceable"><I>!</I></CODE> (an exclamationpoint)[2]that runs a command, stores its output in a temporary file,then puts the temporary filename on its standard output.You use it with<SPANCLASS="link">backquotes (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN>(<CODECLASS="literal">``</CODE>).Here's how to write the example from the previous section:</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[2] The C shell also uses an exclamation point as its<SPANCLASS="link">history character (<ACLASS="linkend"HREF="ch11_01.htm#UPT-ART-4920"TITLE="The Lessons of History ">11.1</A>, <ACLASS="linkend"HREF="ch11_15.htm"TITLE="Changing C Shell History Characters with histchars ">11.15</A>)</SPAN>,but not if there's a space after the exclamation point.This script doesn't conflict with <EMCLASS="emphasis">csh</EM> history.<EMCLASS="emphasis">bash</EM> uses the exclamation point to reverse the exit status of acommand&nbsp;- but then, if you're using <EMCLASS="emphasis">bash</EM>, you don't need our <EMCLASS="emphasis">!</EM>script.</P></BLOCKQUOTE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>comm `! sort file1` `! sort file2`</B></CODE>                echo.where                foo        newprogramrcsdiff.log                runsedrunsed.new</PRE></BLOCKQUOTE></P><PCLASS="para">Why didn't I use the command line below, without the <CODECLASS="replaceable"><I>!</I></CODE> script?</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>comm `sort file1` `sort file2`</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">That's because the <EMCLASS="emphasis">comm</EM> program (like most UNIX programs)needs filename arguments.Using backquotes by themselves would place the list of names (the sortedcontents of the files <EMCLASS="emphasis">file1</EM> and <EMCLASS="emphasis">file2</EM>) on the<EMCLASS="emphasis">comm</EM> command line.</P><PCLASS="para">To see what's happening, you can usea Bourne shell and set its<SPANCLASS="link"><EMCLASS="emphasis">-x</EM> option (<ACLASS="linkend"HREF="ch08_17.htm"TITLE="verbose and echo Variables Show Quoting ">8.17</A>)</SPAN>;the shellwill display the commands it runs with a <CODECLASS="literal">+</CODE> before each one:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>set -x</B></CODE>$ <CODECLASS="userinput"><B>comm `! sort file1` `! sort file2`</B></CODE>+ ! sort file1 + ! sort file2 + comm /tmp/bang3969 /tmp/bang3971                 echo.where                foo        newprogramrcsdiff.log                runsedrunsed.new</PRE></BLOCKQUOTE></P><PCLASS="para">The script made its<SPANCLASS="link">temporary files (<ACLASS="linkend"HREF="ch21_03.htm"TITLE="Unique Names for Temporary Files ">21.3</A>)</SPAN>in <EMCLASS="emphasis">/tmp</EM>.You should probably remove them.If you're the only one using this script, you might be able to get awaywith a command like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>rm /tmp/bang[1-9]*</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If your system has more than one user, it's safer to use<SPANCLASS="link"><EMCLASS="emphasis">find</EM> (<ACLASS="linkend"HREF="ch17_01.htm#UPT-ART-7410"TITLE="The find Command Is Great; The Problem Is Finding How to Use It">17.1</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find /tmp -name 'bang*' -user</B></CODE> <CODECLASS="replaceable"><I>myname</I></CODE> <CODECLASS="userinput"><B>-exec rm {} \;</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If you use this script much, you might make that cleanup command into an<SPANCLASS="link">alias (<ACLASS="linkend"HREF="ch10_02.htm"TITLE="Aliases for Common Commands ">10.2</A>)</SPAN>or a shell script&nbsp;- or start it<SPANCLASS="link">in the background (<ACLASS="linkend"HREF="ch01_26.htm"TITLE="What a Multiuser System Can Do for You ">1.26</A>)</SPAN>from your<SPANCLASS="link"><EMCLASS="emphasis">.logout</EM> file (<ACLASS="linkend"HREF="ch03_01.htm#UPT-ART-4800"TITLE="Running Commands When You Log Out ">3.1</A>, <ACLASS="linkend"HREF="ch03_02.htm"TITLE="Running Commands at Bourne/Korn Shell Logout ">3.2</A>)</SPAN>.</P><PCLASS="para">Here's the <EMCLASS="emphasis">!</EM> script.Of course, you can change the name to something besides <EMCLASS="emphasis">!</EM> if you want.</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch44_15.htm"TITLE="44.15 Handling Command-Line Arguments in Shell Scripts ">$@</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">&#13;#! /bin/shtemp=/tmp/bang$$case $# in0)  echo &quot;Usage: `basename $0` command [args]&quot; 1&gt;&amp;2    echo $temp    exit 1    ;;*)  &quot;$@&quot; &gt; $temp    echo $temp    ;;esac</PRE></TD></TR></TABLE></P><ACLASS="indexterm"NAME="AUTOID-10852"></A></DIV><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_17.htm"TITLE="9.17 Handling Lots of Text with Temporary Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.17 Handling Lots of Text with Temporary Files "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_19.htm"TITLE="9.19 For the Impatient: Type-Ahead "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.19 For the Impatient: Type-Ahead "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">9.17 Handling Lots of Text with Temporary Files </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.19 For the Impatient: Type-Ahead </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 + -