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

📄 ch09_19.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 9] 9.19 For the Impatient: Type-Ahead </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:20Z"><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_18.htm"TITLE="9.18 Process Substitution "><LINKREL="next"HREF="ch09_20.htm"TITLE="9.20 Too Many Files for the Command Line "></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_18.htm"TITLE="9.18 Process Substitution "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.18 Process Substitution "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_20.htm"TITLE="9.20 Too Many Files for the Command Line "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.20 Too Many Files for the Command Line "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6880">9.19 For the Impatient: Type-Ahead </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-10858"></A>The UNIX shells have a feature called <EMCLASS="emphasis">type-ahead</EM> that allows you to continuetyping while the computer is thinking about something. This isconvenient if you have a sequence of commands that need to run inorder, and you don't like waiting for the last command to finishbefore typing the next one.</P><PCLASS="para">Basically, type-ahead just means that the shell lets you keep typing, evenwhen it's apparently &quot;busy&quot; (i.e., even when you don't have a prompt). You caneven continue typing while the current command (in the<SPANCLASS="link">foreground (<ACLASS="linkend"HREF="ch01_26.htm"TITLE="What a Multiuser System Can Do for You ">1.26</A>)</SPAN>)is spraying data to thescreen&nbsp;- although you may find that confusing.Any furthercommands you type will be executed as soon as the foreground commandfinishes. The easiestway to demonstrate type-ahead is with the<SPANCLASS="link"><EMCLASS="emphasis">sleep</EM> (<ACLASS="linkend"HREF="ch40_02.htm"TITLE="Waiting a Little While: sleep ">40.2</A>)</SPAN><ACLASS="indexterm"NAME="AUTOID-10865"></A>command, which just waits:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>sleep 25</B></CODE><CODECLASS="userinput"><B>lpr article</B></CODE>% %</PRE></BLOCKQUOTE></P><PCLASS="para">This <EMCLASS="emphasis">sleep</EM> command does nothing for 25 seconds. Therefore, youdon't see a prompt after pressing RETURN at the end of the <CODECLASS="literal">sleep</CODE>command line.However,you can type the next command (<EMCLASS="emphasis">lpr</EM>), which will be executed assoon as the <EMCLASS="emphasis">sleep</EM> is finished.The<SPANCLASS="link">terminal driver (<ACLASS="linkend"HREF="ch42_01.htm#UPT-ART-1142"TITLE="Making Sense Out of the Terminal Mess ">42.1</A>)</SPAN>reads thecharacters you type, sticks them in a buffer, and hands them to the shellwhenever the shell is ready.</P><PCLASS="para">What happens if something goes wrong?You can press your<ACLASS="indexterm"NAME="AUTOID-10878"></A><ACLASS="indexterm"NAME="AUTOID-10880"></A><SPANCLASS="link">interrupt key (<ACLASS="linkend"HREF="ch38_09.htm"TITLE="Killing Foreground Jobs ">38.9</A>)</SPAN>(like CTRL-c)at any time to cancel the foreground job.In this case, UNIX willdiscard the type-ahead, rather than execute it.(The same goes for CTRL-z, or any other signal the foregroundjob receives from the terminal.[3])This is <EMCLASS="emphasis">usually</EM> what you want; that is, if you press CTRL-cto terminate the foreground job, you usually don't want to execute anyjobs that you've queued up afterward.Type-ahead isn't only good for giving additional commands. If yourcommand takes a long time to start, you can type its input whileyou're waiting. Here's an example that's about as extreme as you'relikely to see.It uses<ACLASS="indexterm"NAME="AUTOID-10886"></A><SPANCLASS="link"><EMCLASS="emphasis">ftp</EM> (<ACLASS="linkend"HREF="ch52_07.htm"TITLE="Other Ways to Get the Software ">52.7</A>)</SPAN>,a program for connecting to a remote host:</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[3] There are somesituations&nbsp;- like executing commands within an Emacs &quot;shellwindow&quot;-where stopping the foreground command may not flush thetype-ahead commands.</P></BLOCKQUOTE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ftpopen golgonoozaloukides</B></CODE>ftp&gt; Connected to golgonooza.220 golgonooza FTP server ready.Name (golgonooza:mikel): 331 Password required for loukides.Password:</PRE></BLOCKQUOTE></P><PCLASS="para">I managed to enter my first <EMCLASS="emphasis">ftp</EM> command and my login name (whichI knew <EMCLASS="emphasis">ftp</EM> would ask for) before <EMCLASS="emphasis">ftp</EM> started.You probably can't use type-ahead for your password, though I've seensome odd systems on which this would work. Even if it works on yoursystem, you shouldn't try it; <EMCLASS="emphasis">ftp</EM> hasn't had time to turnkeyboard echoing off, so your password will appear on your terminal,where anyone can read it.</P><PCLASS="para">Using type-ahead like this takes some guts; you have to know exactlywhat input your application will want, and when it will want it. Butit's also fun in a perverse sense. You will find occasionalapplications (particularly applications that take over the screen)that don't allow type-ahead. However, there's no way to predict whatwill and what won't. I've seen some Emacs implementations that would letyou start editing the file before the editor &quot;came up&quot; on the screen;I've seen others that wouldn't.[<EMCLASS="emphasis">vi</EM> almost always lets you type commands as it starts. <EMCLASS="emphasis">-JP</EM>&nbsp;]</P><PCLASS="para">I have a really terrible way of using type-ahead-I don't recommend it, but it shows what you can do. Rather than use a <SPANCLASS="link">news reader (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>,I often <EMCLASS="emphasis">cd</EM> to a news directory and use<ACLASS="indexterm"NAME="AUTOID-10904"></A><SPANCLASS="link"><EMCLASS="emphasis">grep</EM> (<ACLASS="linkend"HREF="ch27_01.htm#UPT-ART-7420"TITLE="Different Versions of grep ">27.1</A>)</SPAN>to search forinteresting articles. While I watch pages of <EMCLASS="emphasis">grep</EM> output scroll by, Istart typing a<ACLASS="indexterm"NAME="AUTOID-10910"></A><SPANCLASS="link"><EMCLASS="emphasis">more</EM> (<ACLASS="linkend"HREF="ch25_03.htm"TITLE="Using more to Page Through Files ">25.3</A>)</SPAN>command, using the article numbers that I'minterested in. By the time <EMCLASS="emphasis">grep</EM> has worked through the wholenewsgroup, I've finished the <EMCLASS="emphasis">more</EM> command, and I'm ready to readthe articles that <EMCLASS="emphasis">grep</EM> told me about.(I didn't say this wasn't perverse.And it's easier on terminals that use a slow data rate.)</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</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_18.htm"TITLE="9.18 Process Substitution "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 9.18 Process Substitution "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_20.htm"TITLE="9.20 Too Many Files for the Command Line "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.20 Too Many Files for the Command Line "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">9.18 Process Substitution </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.20 Too Many Files for the Command Line </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 + -