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

📄 ch09_16.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 9] 9.16 Command 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:12Z"><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_15.htm"TITLE="9.15 Throwaway Scripts for Complicated Commands "><LINKREL="next"HREF="ch09_17.htm"TITLE="9.17 Handling Lots of Text with Temporary Files "></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_15.htm"TITLE="9.15 Throwaway Scripts for Complicated Commands "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.15 Throwaway Scripts for Complicated Commands "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_17.htm"TITLE="9.17 Handling Lots of Text with Temporary Files "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.17 Handling Lots of Text with Temporary Files "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6630">9.16 Command Substitution </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-10605"></A><ACLASS="indexterm"NAME="AUTOID-10608"></A>A pair of backquotes (<CODECLASS="literal">``</CODE>) does<EMCLASS="emphasis">command substitution</EM>. This is really useful&nbsp;- itlets you use the standard output from one command as arguments toanother command.</P><PCLASS="para">Here's an example. Assume you want to edit all files in thecurrent directory that containthe word &quot;error.&quot; Type this:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch15_07.htm"TITLE="15.7 Getting a List of Matching Files with grep -l ">-l</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">$ <CODECLASS="userinput"><B>vi `grep -l error *.c`</B></CODE>3 files to edit&quot;bar.c&quot; 254 lines, 28338 characters   ...$</PRE></TD></TR></TABLE></P><PCLASS="para">But why does this work? How did we build the incantation above?First, think about how you'd do this without using any specialtechniques. You'd use <EMCLASS="emphasis">grep</EM> to find out which commandscontain<ACLASS="indexterm"NAME="AUTOID-10620"></A>the word &quot;error&quot;; then you'd use <EMCLASS="emphasis">vi</EM>to edit this list:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>grep error *.c</B></CODE>bar.c:  error(&quot;input too long&quot;);bar.c:  error(&quot;input too long&quot;);baz.c:  error(&quot;data formatted incorrectly&quot;);foo.c:  error(&quot;can't divide by zero&quot;):foo.c:  error(&quot;insufficient memory&quot;):$ <CODECLASS="userinput"><B>vi bar.c baz.c foo.c</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Is there any way to compress these into one command? Yes, by usingcommand substitution. First, we need to modify our <EMCLASS="emphasis">grep</EM> commandso that it only produces a list of filenames, rather than filenamesand text. That's easy; use <EMCLASS="emphasis">grep -l</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>grep -l error *.c</B></CODE>bar.cbaz.cfoo.c</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-10634"></A>The <EMCLASS="emphasis">-l</EM> option lists each filename only once, evenif many lines in the file match. (This makes me think that<EMCLASS="emphasis">grep -l</EM> was designed with precisely thisapplication in mind.) Now, we want toedit these files; so we put the <EMCLASS="emphasis">grep</EM> command inside backquotes, and use it as the argument to <EMCLASS="emphasis">vi</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>vi `grep -l error *.c`</B></CODE>3 files to edit&quot;bar.c&quot; 254 lines, 28338 characters   ...$</PRE></BLOCKQUOTE></P><PCLASS="para">You might be wondering about the difference betweenthe &quot;vertical&quot; output from <EMCLASS="emphasis">grep</EM>,and the &quot;horizontal&quot; way that people usuallytype arguments on a command line. The shell handles thiswith no problems. Inside backquotes, both a newline anda space are argument separators.</P><PCLASS="para">The list you use with command substitution doesn't have to be filenames.Let's see how to send a<SPANCLASS="link">mail message (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>to all the users logged on to the system now.You want a command line like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>mail joe lisa franka mondo bozo harpo ...</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Getting there takes a little thinking about what UNIX commands you needto run to get the output you want.(This is real &quot;Power Tools&quot; stuff!)To get a list of those users, you could use<ACLASS="indexterm"NAME="AUTOID-10652"></A><SPANCLASS="link"><EMCLASS="emphasis">who</EM> (<ACLASS="linkend"HREF="ch51_04.htm"TITLE="Who's On? ">51.4</A>)</SPAN>.The <EMCLASS="emphasis">who</EM> output also lists login time and other information&nbsp;- butyou can cut that off with a command like<SPANCLASS="link"><EMCLASS="emphasis">cut</EM> (<ACLASS="linkend"HREF="ch35_14.htm"TITLE="Cutting Columns or Fields with cut ">35.14</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>who | cut -c1-8</B></CODE>joelisafrankalisajoemondojoe...</PRE></BLOCKQUOTE></P><PCLASS="para">Some users are logged on more than once.To get a unique list, use<SPANCLASS="link"><EMCLASS="emphasis">sort -u</EM> (<ACLASS="linkend"HREF="ch36_06.htm"TITLE="Miscellaneous sort Hints ">36.6</A>)</SPAN>.You're done.Just put the name-making command line between backquotes:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>mail `who | cut -c1-8 | sort -u`</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">If you aren't sure how this works, replace the command youwant to run with<SPANCLASS="link"><EMCLASS="emphasis">echo</EM> (<ACLASS="linkend"HREF="ch08_06.htm"TITLE="Output Command-Line Arguments ">8.6</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo `who | cut -c1-8 | sort -u`</B></CODE>bozo franka harpo joe lisa mondo</PRE></BLOCKQUOTE></P><PCLASS="para">After using UNIX for awhile, you'll find that this is one of its mostuseful features. You'll find many situations where youuse one command to generate a list of words, then put that command inbackquotes and use it as an argument to something else.Sometimes you'll want to<SPANCLASS="link">nest (<ACLASS="linkend"HREF="ch45_31.htm"TITLE="Nested Command Substitution ">45.31</A>)</SPAN>the backquotes&nbsp;- this is where the <EMCLASS="emphasis">bash</EM> and <EMCLASS="emphasis">ksh</EM> <CODECLASS="literal">$(&nbsp;)</CODE>operators (which replace the opening and closing backquote,respectively) come in handy.There are<SPANCLASS="link">some problems (<ACLASS="linkend"HREF="ch09_20.htm"TITLE="Too Many Files for the Command Line ">9.20</A>)</SPAN>with command substitution but you usually won't run into them.</P><PCLASS="para">This book has many, many examples of command substitution.Here are some of them:<SPANCLASS="link">making unique filenames (<ACLASS="linkend"HREF="ch16_16.htm"TITLE="Picking a Unique Filename Automatically ">16.16</A>)</SPAN>,<SPANCLASS="link">removing some files from a list (<ACLASS="linkend"HREF="ch23_21.htm"TITLE="Removing Every File but One ">23.21</A>)</SPAN>,<SPANCLASS="link">counting words (<ACLASS="linkend"HREF="ch29_06.htm"TITLE="Counting Lines, Words, and Characters: wc ">29.6</A>)</SPAN>,<SPANCLASS="link">getting a list of files (<ACLASS="linkend"HREF="ch15_09.htm"TITLE="nom: List Files that Don't Match a Wildcard ">15.9</A>)</SPAN>,<SPANCLASS="link">setting your shell prompt (<ACLASS="linkend"HREF="ch07_04.htm"TITLE="Faster Prompt Setting with Built-Ins ">7.4</A>, <ACLASS="linkend"HREF="ch07_06.htm"TITLE="Session Information in Your Terminal's Status Line ">7.6</A>, <ACLASS="linkend"HREF="ch07_11.htm"TITLE="dirs in Your Prompt: Better than $cwd ">7.11</A>)</SPAN>,<SPANCLASS="link">setting variables (<ACLASS="linkend"HREF="ch05_04.htm"TITLE="Setting the TERMCAP Variable with tset ">5.4</A>, <ACLASS="linkend"HREF="ch45_30.htm"TITLE="Grabbing Parts of a String ">45.30</A>)</SPAN>,<SPANCLASS="link">making a wildcard (<ACLASS="linkend"HREF="ch15_05.htm"TITLE='Matching All "Dot Files&quot; with Wildcards'>15.5</A>)</SPAN>,and<SPANCLASS="link">running a loop (<ACLASS="linkend"HREF="ch40_02.htm"TITLE="Waiting a Little While: sleep ">40.2</A>)</SPAN>.</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_15.htm"TITLE="9.15 Throwaway Scripts for Complicated Commands "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.15 Throwaway Scripts for Complicated Commands "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_17.htm"TITLE="9.17 Handling Lots of Text with Temporary Files "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.17 Handling Lots of Text with Temporary Files "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">9.15 Throwaway Scripts for Complicated Commands </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.17 Handling Lots of Text with Temporary Files </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 + -