📄 process-sub.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Process Substitution</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Advanced Topics"HREF="part4.html"><LINKREL="PREVIOUS"TITLE="Restricted Shells"HREF="restricted-sh.html"><LINKREL="NEXT"TITLE="Functions"HREF="functions.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="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLEWIDTH="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="restricted-sh.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="functions.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="PROCESS-SUB">Chapter 22. Process Substitution</A></H1><P><ANAME="PROCESSSUBREF"></A><TTCLASS="REPLACEABLE"><I>Process substitution</I></TT> is the counterpart to <AHREF="commandsub.html#COMMANDSUBREF">command substitution</A>. Command substitution sets a variable to the result of a command, as in <BCLASS="COMMAND">dir_contents=`ls -al`</B> or <BCLASS="COMMAND">xref=$( grep word datafile)</B>. Process substitution feeds the output of a process to another process (in other words, it sends the results of a command to another command).</P><DIVCLASS="VARIABLELIST"><P><B><ANAME="COMMANDSPARENS1"></A>Command substitution template</B></P><DL><DT>command within parentheses</DT><DD><P><BCLASS="COMMAND">>(command)</B></P><P><BCLASS="COMMAND"><(command)</B></P><P>These initiate process substitution. This uses <TTCLASS="FILENAME">/dev/fd/<n></TT> files to send the results of the process within parentheses to another process. <ANAME="AEN14032"HREF="#FTN.AEN14032">[1]</A> </P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>There is <ICLASS="EMPHASIS">no</I> space between the the <SPANCLASS="QUOTE">"<"</SPAN> or <SPANCLASS="QUOTE">">"</SPAN> and the parentheses. Space there would give an error message.</P></TD></TR></TABLE></DIV></DD></DL></DIV><P> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo >(true)</B></TT> <TTCLASS="COMPUTEROUTPUT">/dev/fd/63</TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo <(true)</B></TT> <TTCLASS="COMPUTEROUTPUT">/dev/fd/63</TT> </PRE></TD></TR></TABLE> Bash creates a pipe with two <AHREF="io-redirection.html#FDREF">file descriptors</A>, <TTCLASS="FILENAME">--fIn</TT> and <TTCLASS="FILENAME">fOut--</TT>. The <TTCLASS="FILENAME">stdin</TT> of <AHREF="internal.html#TRUEREF">true</A> connects to <TTCLASS="FILENAME">fOut</TT> (dup2(fOut, 0)), then Bash passes a <TTCLASS="FILENAME">/dev/fd/fIn</TT> argument to <BCLASS="COMMAND">echo</B>. On systems lacking <TTCLASS="FILENAME">/dev/fd/<n></TT> files, Bash may use temporary files. (Thanks, S.C.)</P><P>Process substitution can compare the output of two different commands, or even the output of different options to the same command.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>comm <(ls -l) <(ls -al)</B></TT> <TTCLASS="COMPUTEROUTPUT">total 12-rw-rw-r-- 1 bozo bozo 78 Mar 10 12:58 File0-rw-rw-r-- 1 bozo bozo 42 Mar 10 12:58 File2-rw-rw-r-- 1 bozo bozo 103 Mar 10 12:58 t2.sh total 20 drwxrwxrwx 2 bozo bozo 4096 Mar 10 18:10 . drwx------ 72 bozo bozo 4096 Mar 10 17:58 .. -rw-rw-r-- 1 bozo bozo 78 Mar 10 12:58 File0 -rw-rw-r-- 1 bozo bozo 42 Mar 10 12:58 File2 -rw-rw-r-- 1 bozo bozo 103 Mar 10 12:58 t2.sh</TT></PRE></TD></TR></TABLE><P> Using process substitution to compare the contents of two directories (to see which filenames are in one, but not the other): <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 diff <(ls $first_directory) <(ls $second_directory)</PRE></TD></TR></TABLE> </P><P>Some other usages and uses of process substitution:</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 cat <(ls -l) 2 # Same as ls -l | cat 3 4 sort -k 9 <(ls -l /bin) <(ls -l /usr/bin) <(ls -l /usr/X11R6/bin) 5 # Lists all the files in the 3 main 'bin' directories, and sorts by filename. 6 # Note that three (count 'em) distinct commands are fed to 'sort'. 7 8 9 diff <(command1) <(command2) # Gives difference in command output. 10 11 tar cf >(bzip2 -c > file.tar.bz2) $directory_name 12 # Calls "tar cf /dev/fd/?? $directory_name", and "bzip2 -c > file.tar.bz2". 13 # 14 # Because of the /dev/fd/<n> system feature, 15 # the pipe between both commands does not need to be named. 16 # 17 # This can be emulated. 18 # 19 bzip2 -c < pipe > file.tar.bz2& 20 tar cf pipe $directory_name 21 rm pipe 22 # or 23 exec 3>&1 24 tar cf /dev/fd/4 $directory_name 4>&1 >&3 3>&- | bzip2 -c > file.tar.bz2 3>&- 25 exec 3>&- 26 27 28 # Thanks, St磒hane Chazelas</PRE></TD></TR></TABLE></P><P>A reader sent in the following interesting example of process substitution.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 # Script fragment taken from SuSE distribution: 2 3 while read des what mask iface; do 4 # Some commands ... 5 done < <(route -n) 6 7 8 # To test it, let's make it do something. 9 while read des what mask iface; do 10 echo $des $what $mask $iface 11 done < <(route -n) 12 13 # Output: 14 # Kernel IP routing table 15 # Destination Gateway Genmask Flags Metric Ref Use Iface 16 # 127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo 17 18 19 20 # As St閜hane Chazelas points out, an easier-to-understand equivalent is: 21 route -n | 22 while read des what mask iface; do # Variables set from output of pipe. 23 echo $des $what $mask $iface 24 done # This yields the same output as above. 25 # However, as Ulrich Gayer points out . . . 26 #+ this simplified equivalent uses a subshell for the while loop, 27 #+ and therefore the variables disappear when the pipe terminates. 28 29 30 31 # However, Filip Moritz comments that there is a subtle difference 32 #+ between the above two examples, as the following shows. 33 34 ( 35 route -n | while read x; do ((y++)); done 36 echo $y # $y is still unset 37 38 while read x; do ((y++)); done < <(route -n) 39 echo $y # $y has the number of lines of output of route -n 40 ) 41 42 More generally spoken 43 ( 44 : | x=x 45 # seems to start a subshell like 46 : | ( x=x ) 47 # while 48 x=x < <(:) 49 # does not 50 ) 51 52 # This is useful, when parsing csv and the like. 53 # That is, in effect, what the original SuSE code fragment does.</PRE></TD></TR></TABLE></P></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN14032"HREF="process-sub.html#AEN14032">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>This has the same effect as a <AHREF="extmisc.html#NAMEDPIPEREF">named pipe</A> (temp file), and, in fact, named pipes were at one time used in process substitution.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="restricted-sh.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="functions.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Restricted Shells</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part4.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Functions</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -