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

📄 ch30_26.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 30] 30.26 Shell Escapes: Running One UNIX Command While Using Another</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:45:45Z"><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="ch30_01.htm"TITLE="30. vi Tips and Tricks"><LINKREL="prev"HREF="ch30_25.htm"TITLE="30.25 vi -r May not Write Recovered Buffer When You Exit "><LINKREL="next"HREF="ch30_27.htm"TITLE="30.27 vi Compound Searches "></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="ch30_25.htm"TITLE="30.25 vi -r May not Write Recovered Buffer When You Exit "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.25 vi -r May not Write Recovered Buffer When You Exit "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 30<BR>vi Tips and Tricks</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch30_27.htm"TITLE="30.27 vi Compound Searches "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.27 vi Compound Searches "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0031">30.26 Shell Escapes: Running One UNIX Command While Using Another</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-33972"></A><ACLASS="indexterm"NAME="AUTOID-33974"></A>Some UNIX commands, usually interactive commands like <EMCLASS="emphasis">vi</EM>, let you runanother UNIX command temporarily.To do that, you type a special command character&nbsp;- usually an exclamationpoint (<CODECLASS="literal">!</CODE>)-then type the UNIX command line you want to run.In this article, I'll show examples for the <EMCLASS="emphasis">vi</EM> editor.To see if this works on another utility, check its documentation or just trytyping <CODECLASS="literal">!</CODE><CODECLASS="replaceable"><I>UNIXcommand</I></CODE> when the utility is waiting for you totype a command.</P><PCLASS="para">You can run any UNIX command without quitting <EMCLASS="emphasis">vi</EM>. That's handy, forexample, if you want to read your mail or look at some other file...then go back to the file you were editing without losing your place.It's called a &quot;shell escape.&quot;(By the way, there's a another way to do this, called<SPANCLASS="link">job control (<ACLASS="linkend"HREF="ch12_01.htm#UPT-ART-0439"TITLE="Job Control: Work Faster, Stop Runaway Jobs ">12.1</A>)</SPAN>,that works on many UNIX systems with most of their shells.I think that job control is a lot more convenient and flexible thanshell escapes.)</P><PCLASS="para">Let's say you're editing the file named <EMCLASS="emphasis">foo</EM> and you need to run<EMCLASS="emphasis">grep</EM> to get someone's phone number from your phone file. The steps are:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">Be sure you're in command mode (press the ESCkey if you aren't sure).</P></LI><LICLASS="listitem"><PCLASS="para">If you want to run a command that needs the file you're editing,remember to write out your <EMCLASS="emphasis">vi</EM> buffer with the <CODECLASS="literal">:w</CODE> command.(So, you probably wouldn't need to write before the <EMCLASS="emphasis">grep</EM> command below.)Type <CODECLASS="literal">:!</CODE> followed by the UNIX command, then press RETURN.For example:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch14_11.htm"TITLE="14.11 Finding (Anyone's) Home Directory, Quickly ">~</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"><CODECLASS="userinput"><B>:!grep tim </B></CODE><CODECLASS="userinput"><B>~</B></CODE><CODECLASS="userinput"><B>/phone</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para"></P></LI><LICLASS="listitem"><PCLASS="para">The <EMCLASS="emphasis">grep</EM> program will run. When it finishes, <EMCLASS="emphasis">vi</EM> will say:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">[Hit return to continue]</PRE></BLOCKQUOTE></P><PCLASS="para">After you press RETURN,you'll be right back where you were.</P></LI></OL><PCLASS="para">Other examples:</P><DLCLASS="variablelist"><DTCLASS="term"><CODECLASS="literal">:!pg&nbsp;somefile</CODE></DT><DDCLASS="listitem"><PCLASS="para">Page through <EMCLASS="emphasis">somefile</EM> on your screen.</P></DD><DTCLASS="term"><CODECLASS="literal">:!ptroff&nbsp;%</CODE></DT><DDCLASS="listitem"><PCLASS="para">Run this file through the <EMCLASS="emphasis">ptroff</EM> formatter program.<EMCLASS="emphasis">vi</EM> replaces <CODECLASS="literal">%</CODE> with the name of the file you're editing now.</P></DD><DTCLASS="term"><CODECLASS="literal">:!mail</CODE></DT><DDCLASS="listitem"><PCLASS="para">Read your mail.Be careful about this if you were already running the <EMCLASS="emphasis">mail</EM> program, andyou used the command <EMCLASS="emphasis">~v</EM> to edit a message with <EMCLASS="emphasis">vi</EM> frominside the <EMCLASS="emphasis">mail</EM> program.This shell escape starts a<SPANCLASS="link">subshell (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>;it will <EMCLASS="emphasis">not</EM> take you back tothe same <EMCLASS="emphasis">mail</EM> session before you started editing!</P></DD><DTCLASS="term"><CODECLASS="literal">:sh</CODE></DT><DDCLASS="listitem"><PCLASS="para">Start a completely new shell.(If you are using a shell with job control, you'll almost always want to use job controlto <SPANCLASS="link">temporarily suspend <EMCLASS="emphasis">vi</EM> instead (<ACLASS="linkend"HREF="ch12_04.htm"TITLE="Job Control and autowrite: Real Time Savers! ">12.4</A>)</SPAN>!)</P></DD></DL><PCLASS="para">Basically: anything you can do at a shell prompt, you can do with a shellescape.You'll be in asubshellthough, not your original login shell.So, commands like <EMCLASS="emphasis">cd</EM> won't affect the program where you started thesubshell or any other shell.On the bright side, changing directories or resetting anything inyour environment won't affect <EMCLASS="emphasis">vi</EM> or the shell where you started<EMCLASS="emphasis">vi</EM>.Terminating the program you're running in the subshellwill bring you right back where you were.</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="ch30_25.htm"TITLE="30.25 vi -r May not Write Recovered Buffer When You Exit "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.25 vi -r May not Write Recovered Buffer When You Exit "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="ch30_27.htm"TITLE="30.27 vi Compound Searches "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.27 vi Compound Searches "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">30.25 vi -r May not Write Recovered Buffer When You Exit </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">30.27 vi Compound Searches </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 + -