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

📄 ch13_04.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 13] 13.4 Problems Piping to a Pager </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:36:42Z"><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="ch13_01.htm"TITLE="13. Redirecting Input and Output"><LINKREL="prev"HREF="ch13_03.htm"TITLE="13.3 Send (only) Standard Error Down a Pipe "><LINKREL="next"HREF="ch13_05.htm"TITLE="13.5 Redirection in C Shell: Capture Errors, Too? "></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="ch13_03.htm"TITLE="13.3 Send (only) Standard Error Down a Pipe "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 13.3 Send (only) Standard Error Down a Pipe "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 13<BR>Redirecting Input and Output</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch13_05.htm"TITLE="13.5 Redirection in C Shell: Capture Errors, Too? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 13.5 Redirection in C Shell: Capture Errors, Too? "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0093">13.4 Problems Piping to a Pager </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-14239"></A><ACLASS="indexterm"NAME="AUTOID-14242"></A>If your window onto UNIX (terminal, X window, communications program,whatever) doesn't have a way to show you the previous screenful, usinga pager program like<SPANCLASS="link"><EMCLASS="emphasis">more</EM> (<ACLASS="linkend"HREF="ch25_03.htm"TITLE="Using more to Page Through Files ">25.3</A>)</SPAN>,<EMCLASS="emphasis">pg</EM>, or<SPANCLASS="link"><EMCLASS="emphasis">less</EM> (<ACLASS="linkend"HREF="ch25_04.htm"TITLE='The "less&quot; Pager: More than "more&quot;'>25.4</A>)</SPAN>can be mighty handy.But piping to a pager doesn't always work the way you want it to.</P><PCLASS="para">Here's a <EMCLASS="emphasis">grep</EM> command line that searches several files.What's wrong with it?</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>grep &quot;^set&quot; */.cshrc | more</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">That wasn't a fair question because you can't tell what's wrong.The problem (it turns out) is that the files named <EMCLASS="emphasis">barney/.cshrc</EM>,<EMCLASS="emphasis">edie/.cshrc</EM>, and <EMCLASS="emphasis">gail/.cshrc</EM> are<SPANCLASS="link">read-protected (<ACLASS="linkend"HREF="ch22_02.htm"TITLE="Tutorial on File and Directory Permissions ">22.2</A>)</SPAN>.But, as the first part of<ACLASS="xref"HREF="ch13_04.htm#UPT-ART-93-FIG-0"TITLE="Standard Error Bypassing Pipe, Going Through Pipe">Figure 13.1</A><ACLASS="indexterm"NAME="AUTOID-14261"></A><ACLASS="indexterm"NAME="AUTOID-14264"></A>shows, the error messages scroll offyour screen and the pager doesn'tstop them.</P><H4CLASS="figure"><ACLASS="title"NAME="UPT-ART-93-FIG-0">Figure 13.1: Standard Error Bypassing Pipe, Going Through Pipe</A></H4><IMGCLASS="graphic"SRC="figs/0093.gif"ALT="Figure 13.1"><PCLASS="para">Unless your display is reallllly sloooowww, the error messages are lostand you never know they were there.Or the errors are jumbled up with the &quot;good&quot; <EMCLASS="emphasis">grep</EM> output.That's becauseyou've told the shell to send only the standard outputof <EMCLASS="emphasis">grep</EM> to the pager program.<SPANCLASS="link"> And <EMCLASS="emphasis">grep</EM> writes its errors to the standard error ! (<ACLASS="linkend"HREF="ch45_20.htm"TITLE="Overview: Open Files and File Descriptors ">45.20</A>)</SPAN>But both <EMCLASS="emphasis">stdout</EM> and <EMCLASS="emphasis">stderr</EM> go to the screen at once. The errors on <EMCLASS="emphasis">stderr</EM> scroll away with the output from the pager.The pager can't count the lines of errors so it outputs a complete screenful of <EMCLASS="emphasis">stdout</EM> (the &quot;good stuff&quot;).If <EMCLASS="emphasis">grep</EM>'s standard output (from the files it could read) is at least a screenful, as it is here, there are too many lines to fit on thescreen-and some lines will scroll off. </P><PCLASS="para">The better way to do this is to combine <EMCLASS="emphasis">grep</EM>'s <EMCLASS="emphasis">stdout</EM> and <EMCLASS="emphasis">stderr</EM> andgive them both to the pager. These command lines (in <EMCLASS="emphasis">csh</EM> and <EMCLASS="emphasis">sh</EM>) both do that:</P><PCLASS="para"> <BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>grep &quot;^set&quot; */.cshrc |&amp; more</B></CODE> $ <CODECLASS="userinput"><B>grep &quot;^set&quot; */.cshrc 2&gt;&amp;1 | more</B></CODE></PRE></BLOCKQUOTE> </P><PCLASS="para">The second part of <ACLASS="xref"HREF="ch13_04.htm#UPT-ART-93-FIG-0"TITLE="Standard Error Bypassing Pipe, Going Through Pipe">Figure 13.1</A>shows how this works. Any time I pipe a command's output to a pager, I usually combinethe <EMCLASS="emphasis">stdout</EM> and <EMCLASS="emphasis">stderr</EM> this way. </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="ch13_03.htm"TITLE="13.3 Send (only) Standard Error Down a Pipe "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 13.3 Send (only) Standard Error Down a Pipe "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="ch13_05.htm"TITLE="13.5 Redirection in C Shell: Capture Errors, Too? "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 13.5 Redirection in C Shell: Capture Errors, Too? "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">13.3 Send (only) Standard Error Down a Pipe </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">13.5 Redirection in C Shell: Capture Errors, Too? </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 + -