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

📄 ch10_03.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 10] 10.3 C Shell Aliases with Command-Line Arguments </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:46Z"><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="ch10_01.htm"TITLE="10. Aliases"><LINKREL="prev"HREF="ch10_02.htm"TITLE="10.2 Aliases for Common Commands "><LINKREL="next"HREF="ch10_04.htm"TITLE="10.4 Aliases in ksh and bash "></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="ch10_02.htm"TITLE="10.2 Aliases for Common Commands "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 10.2 Aliases for Common Commands "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 10<BR>Aliases</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch10_04.htm"TITLE="10.4 Aliases in ksh and bash "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 10.4 Aliases in ksh and bash "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-2341">10.3 C Shell Aliases with Command-Line Arguments </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11786"></A><ACLASS="indexterm"NAME="AUTOID-11789"></A><ACLASS="indexterm"NAME="AUTOID-11792"></A>It's convenient for your aliases to use command-line arguments. Forexample, let's think about an alias named <EMCLASS="emphasis">phone</EM>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias phone 'cat ~/phonelist | grep -i'</PRE></BLOCKQUOTE></P><PCLASS="para">After you define that alias, you could type <EMCLASS="emphasis">phone smith</EM>.The shell would find the <EMCLASS="emphasis">phone</EM> alias and execute it<SPANCLASS="link">with the argument (<EMCLASS="emphasis">smith</EM>) at the end (<ACLASS="linkend"HREF="ch10_02.htm"TITLE="Aliases for Common Commands ">10.2</A>)</SPAN>this way:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">cat ~/phonelist | grep -i smith</PRE></BLOCKQUOTE></P><PCLASS="para">Using <EMCLASS="emphasis">cat</EM> and a pipe that way is<SPANCLASS="link">inefficient (<ACLASS="linkend"HREF="ch13_02.htm"TITLE="One Argument with a cat Isn't Enough ">13.2</A>)</SPAN>.It might be more sensible to have an alias that worked likethis:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">grep -i <CODECLASS="replaceable"><I>name</I></CODE> ~/phonelist</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11812"></A><ACLASS="indexterm"NAME="AUTOID-11815"></A>How do we do this?The C shell's<SPANCLASS="link">history (<ACLASS="linkend"HREF="ch11_07.htm"TITLE="History Substitutions ">11.7</A>)</SPAN>facility lets us use thenotation <CODECLASS="literal">!$</CODE> to refer to the last word in the previous command;the notation <CODECLASS="literal">!*</CODE> refers to all the arguments of the previous command.Assuming that we only want to look up aliases one at a time, we canuse <CODECLASS="literal">!$</CODE> and write our alias like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias phone grep -i \!$ ~/phonelist</PRE></BLOCKQUOTE></P><PCLASS="para">When we use the <EMCLASS="emphasis">phone</EM> command, its final argument will be substitutedinto the alias. That is, when we type <CODECLASS="literal">phone&nbsp;bill</CODE>, the shellexecutes the command <CODECLASS="literal">grep&nbsp;-i&nbsp;bill&nbsp;~/phonelist</CODE>.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11830"></A>In this example, we needed another kind of quoting. We had to put abackslash before the exclamation point to prevent the shell fromreplacing <CODECLASS="literal">!$</CODE> with the previous command's last argument. That is, wedon't want the shell to expand <CODECLASS="literal">!$</CODE> when we define the alias&nbsp;- that'snonsense. We want the shell to insert the previous argument when weuse the alias (in which case, the previous argument is just theargument for the alias itself&nbsp;- clear?).</P><PCLASS="para">But why couldn't we just use <SPANCLASS="link">single quotes or double quotes (<ACLASS="linkend"HREF="ch08_14.htm"TITLE="Bourne Shell Quoting ">8.14</A>)</SPAN>?This isn't the right place for a full explanation, but neither single quotes nordouble quotes protect the exclamation point.<SPANCLASS="link"> The backslash does . (<ACLASS="linkend"HREF="ch08_15.htm"TITLE="Differences Between Bourne and C Shell Quoting ">8.15</A>)</SPAN>If you want to be convinced, experiment withsome commands like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>echo '!!'   </B></CODE><EMCLASS="emphasis">Print your last command</EM>% <CODECLASS="userinput"><B>echo '\!!'   </B></CODE><EMCLASS="emphasis">Print !!</EM></PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11845"></A>The first <EMCLASS="emphasis">echo</EM> command shows that the shell performs historysubstitution (i.e., replaces <CODECLASS="literal">!!</CODE> with your previous command) in spiteof the single quotes. The second example shows that the backslash canprevent the shell from interpreting <CODECLASS="literal">!</CODE> as a special character.</P><PCLASS="para">Let's look at another alias. We want to pipe the output of <EMCLASS="emphasis">ls -l</EM> into<SPANCLASS="link"><EMCLASS="emphasis">more</EM> (<ACLASS="linkend"HREF="ch25_03.htm"TITLE="Using more to Page Through Files ">25.3</A>)</SPAN>.In this case, we would want all the argumentsfrom the command line instead of merely the last argument (or theonly argument). Here's the alias:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias lm 'ls -l \!* | more'</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11857"></A><ACLASS="indexterm"NAME="AUTOID-11860"></A>This time, we needed both kinds of quoting:A backslash prevents the shell from interpreting the exclamation pointimmediately.Single quotes protect the pipe symbol and the asterisk (<CODECLASS="literal">*</CODE>).If you don't protect them both, and only protect the pipe (with abackslash), look what happens:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>alias lm ls -l \!*  more</B></CODE>alias: No match.</PRE></BLOCKQUOTE></P><PCLASS="para">Because the backslash temporarily stops the special meaning of the <CODECLASS="literal">!</CODE>,the shell next tries to find filenames that match the<SPANCLASS="link">wildcard (<ACLASS="linkend"HREF="ch01_16.htm"TITLE="Wildcards ">1.16</A>)</SPAN>pattern <CODECLASS="literal">!*</CODE>.That fails (except in the unusual case when you have a file in thecurrent directory whose name starts with a <CODECLASS="replaceable"><I>!</I></CODE>).</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> <ACLASS="indexterm"NAME="AUTOID-11874"></A><ACLASS="indexterm"NAME="AUTOID-11877"></A>Here's a good general rule for quoting aliases.Unless you're trying to do something specialwith an alias and you understand quoting well, put single quotes(<CODECLASS="literal">'</CODE>) around the whole definition and put a backslash beforeevery exclamation point (<CODECLASS="literal">/!</CODE>).</P></BLOCKQUOTE><PCLASS="para">Finally, if you want to pick one argument from the command line, use<CODECLASS="literal">/!:</CODE><CODECLASS="replaceable"><I>n</I></CODE>, where <CODECLASS="replaceable"><I>n</I></CODE> is the number of the argument. Here's one finalalias.It uses<SPANCLASS="link"><EMCLASS="emphasis">cat</EM> (<ACLASS="linkend"HREF="ch25_02.htm"TITLE="Four Ways to Skin a cat ">25.2</A>)</SPAN>to add a header file to the file named in the first argument, thenwrites them both into the file named in the second argument:</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">alias addhead 'cat ~/txt/header \!:1 &gt; \!:2'</PRE></TD></TR></TABLE></P><PCLASS="para">This alias has two arguments: the file to which you want to add aheader and the output file. When you type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>addhead foo bar</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">&#13;the C shell substitutes the filename <CODECLASS="literal">foo</CODE> for <CODECLASS="literal">/!:1</CODE>, and thefilename <CODECLASS="literal">bar</CODE> for <CODECLASS="literal">/!:2</CODE>, executing the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">cat ~/txt/header foo &gt; bar</PRE></BLOCKQUOTE></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN>, <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="ch10_02.htm"TITLE="10.2 Aliases for Common Commands "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 10.2 Aliases for Common Commands "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="ch10_04.htm"TITLE="10.4 Aliases in ksh and bash "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 10.4 Aliases in ksh and bash "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">10.2 Aliases for Common Commands </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">10.4 Aliases in ksh and bash </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 + -