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

📄 ch09_20.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 9] 9.20 Too Many Files for the Command Line </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:23Z"><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_19.htm"TITLE="9.19 For the Impatient: Type-Ahead "><LINKREL="next"HREF="ch09_21.htm"TITLE="9.21 Handle Too-Long Command Lines with xargs "></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_19.htm"TITLE="9.19 For the Impatient: Type-Ahead "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.19 For the Impatient: Type-Ahead "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_21.htm"TITLE="9.21 Handle Too-Long Command Lines with xargs "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.21 Handle Too-Long Command Lines with xargs "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0001">9.20 Too Many Files for the Command Line </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-10922"></A><ACLASS="indexterm"NAME="UPT-ART-1-IX-TOO-LONG-COMMANDS"></A><ACLASS="indexterm"NAME="UPT-ART-1-IX-TOO-LONG-FOR-COMMAND-LINE"></A>A pair of<SPANCLASS="link">backquotes (<CODECLASS="literal">``</CODE>) (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN>lets you run a command like<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>and put its output onto another command's command line.For example:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch43_07.htm"TITLE="43.7 Quick-and-Dirty Formatting Before Printing ">pr</A> <ACLASS="co"HREF="ch43_02.htm"TITLE="43.2 Introduction to Printing on UNIX ">lpr</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>pr </B></CODE><CODECLASS="userinput"><B>-n `find . -type f -mtime -1 -print` | lpr</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">would give a list of all the files you edited today to <EMCLASS="emphasis">pr</EM> andpipe <EMCLASS="emphasis">pr</EM>'s output to the printer.</P><PCLASS="para">One day I was making<SPANCLASS="link">global substitutions to a lot of files (<ACLASS="linkend"HREF="ch34_03.htm"TITLE="Testing and Using a sed Script: checksed, runsed ">34.3</A>, <ACLASS="linkend"HREF="ch28_09.htm"TITLE="ex Scripts Built by diff ">28.9</A>)</SPAN><ACLASS="indexterm"NAME="AUTOID-10946"></A><ACLASS="indexterm"NAME="AUTOID-10949"></A>and got the error <CODECLASS="literal">Arguments too long</CODE>when I tried to print the files I had edited.Turned out that <EMCLASS="emphasis">find</EM> output such a long list of files that itoverflowed the command line:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>pr -n ./</B></CODE><CODECLASS="replaceable"><I>path</I></CODE><CODECLASS="userinput"><B>/</B></CODE><CODECLASS="replaceable"><I>file1 </I></CODE><CODECLASS="userinput"><B>./</B></CODE><CODECLASS="replaceable"><I>path</I></CODE><CODECLASS="userinput"><B>/</B></CODE><CODECLASS="replaceable"><I>path</I></CODE><CODECLASS="userinput"><B>/</B></CODE><CODECLASS="replaceable"><I>file2 </I></CODE><CODECLASS="userinput"><B>./</B></CODE><CODECLASS="replaceable"><I>path</I></CODE><CODECLASS="userinput"><B>/</B></CODE><CODECLASS="replaceable"><I>file3</I></CODE> <CODECLASS="userinput"><B>... | lpr</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">(This can happen for any command inside backquotes, not just <EMCLASS="emphasis">find</EM>.)</P><PCLASS="para">I had to split <EMCLASS="emphasis">find</EM>'s standard output into chunks that wouldn'tbe too long.<ACLASS="indexterm"NAME="AUTOID-10975"></A>This was on a UNIX that didn't have the great<SPANCLASS="link"><EMCLASS="emphasis">xargs</EM> (<ACLASS="linkend"HREF="ch09_21.htm"TITLE="Handle Too-Long Command Lines with xargs ">9.21</A>)</SPAN>,so I decided to use<SPANCLASS="link"><EMCLASS="emphasis">fmt</EM> (<ACLASS="linkend"HREF="ch35_02.htm"TITLE="Neatening Text with fmt ">35.2</A>)</SPAN>instead.<EMCLASS="emphasis">fmt</EM> reads its standard input and collects enough text to fill anoutput line.I used <EMCLASS="emphasis">fmt -1000</EM>,which makes output lines about 1000 characterswide-long enough so I wouldn't need too many <EMCLASS="emphasis">pr</EM> commands, butnot too long...I started a Bourne shell, which lets you<SPANCLASS="link">pipe to the input of a loop (<ACLASS="linkend"HREF="ch45_23.htm"TITLE="The Ins and Outs of Redirected I/O Loops ">45.23</A>)</SPAN>.The shell prints<SPANCLASS="link">secondary prompts (<ACLASS="linkend"HREF="ch09_13.htm"TITLE="Multiline Commands, Secondary Prompts ">9.13</A>)</SPAN>until you finish entering the loop:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch44_13.htm"TITLE="44.13 read: Reading from the Keyboard ">read</A> <ACLASS="co"HREF="ch38_04.htm"TITLE="38.4 Subshells ">exit</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>sh</B></CODE>$ <CODECLASS="userinput"><B>find . -type f -mtime -1 -print |</B></CODE>&gt; <CODECLASS="userinput"><B>fmt -1000 |</B></CODE>&gt; <CODECLASS="userinput"><B>while </B></CODE><CODECLASS="userinput"><B>read </B></CODE><CODECLASS="userinput"><B>files</B></CODE>&gt; <CODECLASS="userinput"><B>do pr -n $files</B></CODE>&gt; <CODECLASS="userinput"><B>done | lpr</B></CODE>$ <CODECLASS="userinput"><B>exit</B></CODE>%</PRE></TD></TR></TABLE></P><PCLASS="para">The shell put each line of filenames from <EMCLASS="emphasis">fmt -1000</EM> into the<EMCLASS="emphasis">files</EM> shell variable, ran <EMCLASS="emphasis">pr</EM> with those filenames, andpiped the output of all the <EMCLASS="emphasis">pr</EM>s to the standard input of <EMCLASS="emphasis">lpr</EM>.The <EMCLASS="emphasis">lpr</EM> command didn't know that it was being fed by lots of <EMCLASS="emphasis">pr</EM>commands&nbsp;- all it saw was a series of 66&nbsp;- line pages that the <EMCLASS="emphasis">pr</EM>soutput.</P><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-1-IX-XARGS-COMMAND"></A>If you have<EMCLASS="emphasis">xargs</EM>on your system, you can do the same thing this way:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>find . -type f -mtime -1 -print | xargs pr -n | lpr</B></CODE></PRE></BLOCKQUOTE></P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="xref.warning"HREF="ch09_22.htm"TITLE="9.22 xargs: Problems with Spaces and Newlines ">Warning!</A> </TH><TDVALIGN="TOP">&#13;<EMCLASS="emphasis">xargs</EM> reads text from its standard input, collects a reasonable amount,then runs the command line <CODECLASS="literal">pr&nbsp;-n&nbsp;</CODE><CODECLASS="replaceable"><I>path</I></CODE><CODECLASS="literal">/</CODE><CODECLASS="replaceable"><I>file&nbsp;path</I></CODE><CODECLASS="literal">/</CODE><CODECLASS="replaceable"><I>file</I></CODE><CODECLASS="literal">...</CODE>.Then <EMCLASS="emphasis">xargs</EM> reads more text and runs <EMCLASS="emphasis">pr</EM> again, over and over,until it's read all the text.The output of the <EMCLASS="emphasis">xargs</EM> command (which is actually the output ofall those <EMCLASS="emphasis">pr</EM>s) is fed to a single <EMCLASS="emphasis">lpr</EM> command.</TD></TR></TABLE><PCLASS="para">Parting shot (by ML): there's really no excuse for <EMCLASS="emphasis">xargs</EM> or any of these other tricks;they're just a patch for a design error. UNIX should be ableto handle arbitrarily long command lines; maybe in some futureversion, it will.</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_19.htm"TITLE="9.19 For the Impatient: Type-Ahead "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.19 For the Impatient: Type-Ahead "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_21.htm"TITLE="9.21 Handle Too-Long Command Lines with xargs "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.21 Handle Too-Long Command Lines with xargs "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">9.19 For the Impatient: Type-Ahead </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.21 Handle Too-Long Command Lines with xargs </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 + -