📄 ch09_21.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 9] 9.21 Handle Too-Long Command Lines with xargs </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:27Z"><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_20.htm"TITLE="9.20 Too Many Files for the Command Line "><LINKREL="next"HREF="ch09_22.htm"TITLE="9.22 xargs: Problems with Spaces and Newlines "></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_20.htm"TITLE="9.20 Too Many Files for the Command Line "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.20 Too Many Files for the Command Line "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_22.htm"TITLE="9.22 xargs: Problems with Spaces and Newlines "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.22 xargs: Problems with Spaces and Newlines "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-5490">9.21 Handle Too-Long Command Lines with xargs </A></H2><PCLASS="para"><EMCLASS="emphasis">xargs</EM> is one of those UNIX utilities that seems pretty useless whenyou first hear about it - but turns into one of the handiest tools youcan have.</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="xargs">xargs</A><BR></TH><TDVALIGN="TOP"> If your system doesn't already have <EMCLASS="emphasis">xargs</EM>, be sure to installit from the CD-ROM.</TD></TR></TABLE><PCLASS="para"><EMCLASS="emphasis">xargs</EM> reads a group of arguments from its standard input, then runs a UNIX command with that group of arguments.It keeps reading arguments and running the command until it runs out ofarguments.The shell's<SPANCLASS="link">backquotes (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN>do the same kind of thing, but they give all the arguments to the commandat once.This can give you a<SPANCLASS="link"><CODECLASS="literal">Too many arguments</CODE> (<ACLASS="linkend"HREF="ch09_20.htm"TITLE="Too Many Files for the Command Line ">9.20</A>)</SPAN>error.</P><PCLASS="para">Here are a couple of examples:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">If you want to print most of the files in a large directory,put the output of <EMCLASS="emphasis">ls</EM> into a file.Edit the file to leave just the filenames you want printed.Give the file to <EMCLASS="emphasis">xargs</EM>' standard input:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch13_01.htm"TITLE="13.1 Using Standard Input and Output"><</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls > allfiles.tmp</B></CODE>% <CODECLASS="userinput"><B>vi allfiles.tmp</B></CODE>% <CODECLASS="userinput"><B>xargs lpr < allfiles.tmp</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">What did that do?With lines like these in <EMCLASS="emphasis">allfiles.tmp</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>cat allfiles.tmp</B></CODE>afileapplication ...yoyotestzapme</PRE></BLOCKQUOTE></P><PCLASS="para"><EMCLASS="emphasis">xargs</EM> ran one or more <EMCLASS="emphasis">lpr</EM> commands, each with a group ofarguments, until it had read every word in the file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">lpr afile application ... ...lpr ... yoyotest zapme</PRE></BLOCKQUOTE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">The standard output of <EMCLASS="emphasis">xargs</EM> is the standard output of thecommands it runs.So, if you'd created <EMCLASS="emphasis">allfiles.tmp</EM> above but you wanted to formatthe files with<SPANCLASS="link"><EMCLASS="emphasis">pr</EM> (<ACLASS="linkend"HREF="ch43_07.htm"TITLE="Quick-and-Dirty Formatting Before Printing ">43.7</A>)</SPAN>first, you could type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>xargs pr < allfiles.tmp | lpr</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Then <EMCLASS="emphasis">xargs</EM> would run all of these <EMCLASS="emphasis">pr</EM> commands.The shell would pipe their standard outputs[4]to a single <EMCLASS="emphasis">lpr</EM> command:</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[4] Actually, the shell is piping the standard output of <EMCLASS="emphasis">xargs</EM>.As I said above, <EMCLASS="emphasis">xargs</EM> sends the standard output of commands it runsto its own standard output.</P></BLOCKQUOTE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">pr afile application ... ...pr ... yoyotest zapme</PRE></BLOCKQUOTE></P><PCLASS="para">In this next example,<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>gets a list of all files in the directory tree.Next, we use <EMCLASS="emphasis">xargs</EM> to read those filenames and run<SPANCLASS="link"><EMCLASS="emphasis">grep -l</EM> (<ACLASS="linkend"HREF="ch15_07.htm"TITLE="Getting a List of Matching Files with grep -l ">15.7</A>)</SPAN>to find which files contain the word "WARNING."Next, we pipe that to a setup with <EMCLASS="emphasis">pr</EM> and <EMCLASS="emphasis">lpr</EM>, like the onein the previous example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -type f -print | xargs grep -l WARNING | xargs pr | lpr</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">"Huh?" you might say.Just take that step by step.The output of <EMCLASS="emphasis">find</EM> is a list of filenames, like <CODECLASS="literal">./afile ./bfile ... ./adir/zfile</CODE> and so on.The first <EMCLASS="emphasis">xargs</EM> gives those filenames to one or more <EMCLASS="emphasis">grep -l</EM>commands:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"> grep -l WARNING ./afile ./bfile ... ...grep -l WARNING ./adir/zfile ...</PRE></BLOCKQUOTE></P><PCLASS="para"> The standard output of all those <EMCLASS="emphasis">grep</EM>s is a (shortened) list offilenames that match.That's piped to another <EMCLASS="emphasis">xargs</EM>-it runs <EMCLASS="emphasis">pr</EM> commands with thefilenames that <EMCLASS="emphasis">grep</EM> found.</P><PCLASS="para">UNIX is weird and wonderful!</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11115"></A>Sometimes you don't want <EMCLASS="emphasis">xargs</EM> to run its command with as manyarguments as it can fit on the command line.The <EMCLASS="emphasis">-n</EM> option sets the maximum number of arguments <EMCLASS="emphasis">xargs</EM>will give to each command.Another handy option, <EMCLASS="emphasis">-p</EM>, prompts you before running each command.</P><PCLASS="para">Here's a directory full of files with errors (whose names end with<EMCLASS="emphasis">.bad</EM>) and corrected versions (named <EMCLASS="emphasis">.fixed</EM>).I use <EMCLASS="emphasis">ls</EM> to give the list of files to <EMCLASS="emphasis">xargs</EM>; it reads twofilenames at once, then asks whether I want to run <EMCLASS="emphasis">diff -c</EM> to comparethose two files.It keeps prompting me and running <EMCLASS="emphasis">diff -c</EM>until it runs out of file pairs:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls</B></CODE>chap1.badchap1.fixedchap2.badchap2.fixed ...chap9.badchap9.fixed% <CODECLASS="userinput"><B>ls | xargs -p -n2 diff -c</B></CODE>diff -c chap1.bad chap1.fixed ?...<CODECLASS="userinput"><B>y</B></CODE> ...<EMCLASS="emphasis">Output of diff command for chap1</EM>...diff -c chap2.bad chap2.fixed ?...<CODECLASS="userinput"><B>n</B></CODE>diff -c chap3.bad chap3.fixed ?...<CODECLASS="userinput"><B>y</B></CODE> ...<EMCLASS="emphasis">Output of diff command for chap3</EM>... ...</PRE></BLOCKQUOTE></P></LI></UL><PCLASS="para">As the<SPANCLASS="link">next article (<ACLASS="linkend"HREF="ch09_22.htm"TITLE="xargs: Problems with Spaces and Newlines ">9.22</A>)</SPAN>explains, <EMCLASS="emphasis">xargs</EM> can have trouble if an argument has white spaceinside a word.Luckily, the GNU <EMCLASS="emphasis">xargs</EM> (read about it there) solves the problem.</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_20.htm"TITLE="9.20 Too Many Files for the Command Line "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.20 Too Many Files for the Command Line "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_22.htm"TITLE="9.22 xargs: Problems with Spaces and Newlines "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.22 xargs: Problems with Spaces and Newlines "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">9.20 Too Many Files for the Command Line </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.22 xargs: Problems with Spaces and Newlines </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 + -