📄 ch09_01.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 9] Saving Time on the Command Line</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:34:51Z"><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="part02.htm"TITLE="II. Let the Computer Do the Dirty Work "><LINKREL="prev"HREF="ch08_20.htm"TITLE="8.20 How Many Backslashes?"><LINKREL="next"HREF="ch09_02.htm"TITLE="9.2 Fix a Line Faster with Line-Kill and Word-Erase "></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="ch08_20.htm"TITLE="8.20 How Many Backslashes?"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.20 How Many Backslashes?"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 9</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch09_02.htm"TITLE="9.2 Fix a Line Faster with Line-Kill and Word-Erase "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.2 Fix a Line Faster with Line-Kill and Word-Erase "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="CHAPTER"><H1CLASS="chapter"><ACLASS="title"NAME="UPT-CHP-9">9. Saving Time on the Command Line</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#UPT-ART-7660"TITLE="9.1 What's Special About the UNIX Command Line">What's Special About the UNIX Command Line</A><BR><ACLASS="sect1"HREF="ch09_02.htm"TITLE="9.2 Fix a Line Faster with Line-Kill and Word-Erase ">Fix a Line Faster with Line-Kill and Word-Erase </A><BR><ACLASS="sect1"HREF="ch09_03.htm"TITLE="9.3 Reprinting Your Command Line with CTRL-r ">Reprinting Your Command Line with CTRL-r </A><BR><ACLASS="sect1"HREF="ch09_04.htm"TITLE="9.4 Use Wildcards to Create Files? ">Use Wildcards to Create Files? </A><BR><ACLASS="sect1"HREF="ch09_05.htm"TITLE="9.5 Build Strings with { } ">Build Strings with { } </A><BR><ACLASS="sect1"HREF="ch09_06.htm"TITLE="9.6 String Editing (Colon) Operators ">String Editing (Colon) Operators </A><BR><ACLASS="sect1"HREF="ch09_07.htm"TITLE="9.7 String Editing in ksh and bash ">String Editing in ksh and bash </A><BR><ACLASS="sect1"HREF="ch09_08.htm"TITLE="9.8 Filename Completion: Faster Filename Typing ">Filename Completion: Faster Filename Typing </A><BR><ACLASS="sect1"HREF="ch09_09.htm"TITLE="9.9 Don't Match Useless Files in Filename Completion ">Don't Match Useless Files in Filename Completion </A><BR><ACLASS="sect1"HREF="ch09_10.htm"TITLE="9.10 Filename Completion Isn't Always the Answer ">Filename Completion Isn't Always the Answer </A><BR><ACLASS="sect1"HREF="ch09_11.htm"TITLE="9.11 Repeating a Command with a foreach Loop ">Repeating a Command with a foreach Loop </A><BR><ACLASS="sect1"HREF="ch09_12.htm"TITLE="9.12 The Bourne Shell for Loop ">The Bourne Shell for Loop </A><BR><ACLASS="sect1"HREF="ch09_13.htm"TITLE="9.13 Multiline Commands, Secondary Prompts ">Multiline Commands, Secondary Prompts </A><BR><ACLASS="sect1"HREF="ch09_14.htm"TITLE="9.14 Using Here Documents for Form Letters, etc. ">Using Here Documents for Form Letters, etc. </A><BR><ACLASS="sect1"HREF="ch09_15.htm"TITLE="9.15 Throwaway Scripts for Complicated Commands ">Throwaway Scripts for Complicated Commands </A><BR><ACLASS="sect1"HREF="ch09_16.htm"TITLE="9.16 Command Substitution ">Command Substitution </A><BR><ACLASS="sect1"HREF="ch09_17.htm"TITLE="9.17 Handling Lots of Text with Temporary Files ">Handling Lots of Text with Temporary Files </A><BR><ACLASS="sect1"HREF="ch09_18.htm"TITLE="9.18 Process Substitution ">Process Substitution </A><BR><ACLASS="sect1"HREF="ch09_19.htm"TITLE="9.19 For the Impatient: Type-Ahead ">For the Impatient: Type-Ahead </A><BR><ACLASS="sect1"HREF="ch09_20.htm"TITLE="9.20 Too Many Files for the Command Line ">Too Many Files for the Command Line </A><BR><ACLASS="sect1"HREF="ch09_21.htm"TITLE="9.21 Handle Too-Long Command Lines with xargs ">Handle Too-Long Command Lines with xargs </A><BR><ACLASS="sect1"HREF="ch09_22.htm"TITLE="9.22 xargs: Problems with Spaces and Newlines ">xargs: Problems with Spaces and Newlines </A><BR><ACLASS="sect1"HREF="ch09_23.htm"TITLE='9.23 Workaround for "Arguments too long" Error'>Workaround for "Arguments too long" Error</A><BR><ACLASS="sect1"HREF="ch09_24.htm"TITLE="9.24 Get File List by Editing Output of ls -l, grep, etc. ">Get File List by Editing Output of ls -l, grep, etc. </A><BR><ACLASS="sect1"HREF="ch09_25.htm"TITLE="9.25 The C Shell repeat Command ">The C Shell repeat Command </A><BR><ACLASS="sect1"HREF="ch09_26.htm"TITLE="9.26 Expect ">Expect </A></P><P></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7660">9.1 What's Special About the UNIX Command Line</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-9661"></A>One of UNIX's best features is the shell's command line. Why? Every modernoperating system has a command line; we don't use card readers withobscure job setup cards any more. What makes UNIX's special?</P><PCLASS="para">The UNIX shell command line allows lots of shortcuts. Some of these you'llfind in other operating systems; some you won't. In this chapter,we'll introduce a lot of these shortcuts.Among other things, we'll discuss:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><SPANCLASS="link">Faster erasing (<ACLASS="linkend"HREF="ch09_02.htm"TITLE="Fix a Line Faster with Line-Kill and Word-Erase ">9.2</A>)</SPAN>of mistakes with the line-kill and word-erase characters.(These aren't just a feature of the shell; they work at many placesother than a shell prompt.)</P></LI><LICLASS="listitem"><PCLASS="para"><SPANCLASS="link">Filename completion (<ACLASS="linkend"HREF="ch09_08.htm"TITLE="Filename Completion: Faster Filename Typing ">9.8</A>, <ACLASS="linkend"HREF="ch09_09.htm"TITLE="Don't Match Useless Files in Filename Completion ">9.9</A>, <ACLASS="linkend"HREF="ch09_10.htm"TITLE="Filename Completion Isn't Always the Answer ">9.10</A>)</SPAN>,which allows you to type the beginning of afilename and let the shell fill in the rest.</P></LI><LICLASS="listitem"><PCLASS="para"><SPANCLASS="link">Command substitution (<ACLASS="linkend"HREF="ch09_16.htm"TITLE="Command Substitution ">9.16</A>)</SPAN>,which lets you use the output from one commandas arguments to another. (Note: this is <EMCLASS="emphasis">different</EM> from<SPANCLASS="link">pipelining (<ACLASS="linkend"HREF="ch01_04.htm"TITLE="Using Pipes to Create a New Tool ">1.4</A>)</SPAN>.)</P></LI><LICLASS="listitem"><PCLASS="para"><SPANCLASS="link">Process substitution (<ACLASS="linkend"HREF="ch09_18.htm"TITLE="Process Substitution ">9.18</A>)</SPAN>in <EMCLASS="emphasis">bash</EM>, and a script named <EMCLASS="emphasis">!</EM> for other shells, lets youput the output of a command into a temporary file - and give that filenameto a process.</P></LI><LICLASS="listitem"><PCLASS="para"><SPANCLASS="link">Type-ahead (<ACLASS="linkend"HREF="ch09_19.htm"TITLE="For the Impatient: Type-Ahead ">9.19</A>)</SPAN>,the ability to type your next command (or commands)while the previous command is still running.</P></LI><LICLASS="listitem"><PCLASS="para">How to handle <SPANCLASS="link">command lines that become too long (<ACLASS="linkend"HREF="ch09_20.htm"TITLE="Too Many Files for the Command Line ">9.20</A>, <ACLASS="linkend"HREF="ch09_21.htm"TITLE="Handle Too-Long Command Lines with xargs ">9.21</A>, <ACLASS="linkend"HREF="ch09_23.htm"TITLE='Workaround for "Arguments too long" Error'>9.23</A>)</SPAN>.</P></LI></UL><PCLASS="para">Some fundamental command-line features that we aren't discussing inthis chapter, but which are discussed elsewhere, are:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><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>,which lets you run several commands at the same time.</P></LI><LICLASS="listitem"><PCLASS="para"><SPANCLASS="link">Aliases (<ACLASS="linkend"HREF="ch10_02.htm"TITLE="Aliases for Common Commands ">10.2</A>)</SPAN>,or abbreviations, for commands.<SPANCLASS="link">Shell functions (<ACLASS="linkend"HREF="ch10_09.htm"TITLE="Shell Functions ">10.9</A>)</SPAN>are similar.</P></LI><LICLASS="listitem"><PCLASS="para"><SPANCLASS="link">Command-line editing (<ACLASS="linkend"HREF="ch11_13.htm"TITLE="Shell Command-Line Editing ">11.13</A>)</SPAN>and<SPANCLASS="link">history substitution (<ACLASS="linkend"HREF="ch11_01.htm#UPT-ART-4920"TITLE="The Lessons of History ">11.1</A>)</SPAN>are two different ways (both useful) to "recall" previous commands.</P></LI><LICLASS="listitem"><PCLASS="para"><SPANCLASS="link">Quoting (<ACLASS="linkend"HREF="ch08_14.htm"TITLE="Bourne Shell Quoting ">8.14</A>, <ACLASS="linkend"HREF="ch08_15.htm"TITLE="Differences Between Bourne and C Shell Quoting ">8.15</A>)</SPAN>,the way you "protect" special characters from the UNIX shell.</P></LI><LICLASS="listitem"><PCLASS="para"><SPANCLASS="link">Wildcards (<ACLASS="linkend"HREF="ch15_02.htm"TITLE="Filename Wildcards in a Nutshell ">15.2</A>)</SPAN>.</P></LI></UL><PCLASS="para">You don't need to be a command-line virtuoso to use UNIX effectively.But you'd be surprised at how much you can do with a few tricks.If all you can do at the command line is type <CODECLASS="literal">ls</CODE> or startFrameMaker, you're missing out on a lot.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN></P></DIV></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="ch08_20.htm"TITLE="8.20 How Many Backslashes?"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.20 How Many Backslashes?"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_02.htm"TITLE="9.2 Fix a Line Faster with Line-Kill and Word-Erase "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 9.2 Fix a Line Faster with Line-Kill and Word-Erase "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">8.20 How Many Backslashes?</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.2 Fix a Line Faster with Line-Kill and Word-Erase </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 + -