📄 ch10_02.htm
字号:
></A>Note that we put this alias in quotes.Why? Because it's a compoundcommand (<EMCLASS="emphasis">setenv</EM>, then <EMCLASS="emphasis">vi</EM>).We want the alias toinclude both stages of the command. Think about what this means if wedon't put quotes around the alias definition when defining the alias:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias vtext setenv EXINIT "source $HOME/.exrc.text" ; vi <EMCLASS="emphasis">Wrong!</EM></PRE></BLOCKQUOTE></P><PCLASS="para">The shell sees the<SPANCLASS="link">semicolon (<ACLASS="linkend"HREF="ch08_05.htm"TITLE="Command-Line Evaluation ">8.5</A>)</SPAN>(a command separator) outside of quotes,so it separates the command line into two commands.The first command defines the <EMCLASS="emphasis">vtext</EM> alias to run <EMCLASS="emphasis">setenv</EM>,not <EMCLASS="emphasis">vi</EM>.After the alias is defined, the shell runs the second command:<EMCLASS="emphasis">vi</EM> with no filename.In any case, the results have nothing to do with what you want.</P><PCLASS="para">The way we originally defined the <EMCLASS="emphasis">vtext</EM> alias, with quotesaround the whole definition, is what we want.The outer quotes tell the shell to put everything into the aliasdefinition.The semicolon in the alias will be interpreted, and separate the two commands,any time you use the alias.</P><PCLASS="para">Next, look at the <EMCLASS="emphasis">clean</EM> alias.As with the <EMCLASS="emphasis">vtext</EM> alias, this one needs to be quoted.Thereason now is a bit different; the quotes prevent the shellfrom expanding the <CODECLASS="literal">*</CODE> wildcard immediately. That is, if you just typed:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>alias clean rm *~</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">the shell would expand the wildcard immediately.So if a file named <EMCLASS="emphasis">foo~</EM> is in your current directory, the<EMCLASS="emphasis">clean</EM> alias will be <CODECLASS="literal">rm</CODE> <CODECLASS="literal">foo~</CODE>.That (most likely) isn't what you want; a<EMCLASS="emphasis">clean</EM> alias that will only delete one particular file isn't veryinteresting.</P><PCLASS="para">So you need a way to prevent the shell from interpreting the <CODECLASS="literal">*</CODE> rightnow (when you define the alias); you want the shell to interpret <CODECLASS="literal">*</CODE>later, when you use the alias.There are plenty of articles in this book about <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>,but thesimplest way to write an alias that uses wildcards (or other specialcharacters) is to put it inside of quotation marks.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-2340-SECT-1.3">10.2.3 Setting Aliases Automatically, Unsetting Aliases </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11739"></A><ACLASS="indexterm"NAME="AUTOID-11742"></A>Any aliases you define can be placed in your <EMCLASS="emphasis">.cshrc</EM> file, sothat they'll be available whenever you're using the C shell.(Note: aliases are<SPANCLASS="link"><EMCLASS="emphasis">not</EM> passed to subprocesses (<ACLASS="linkend"HREF="ch02_02.htm"TITLE="Shell Setup Files-Which, Where, and Why ">2.2</A>)</SPAN>,so putting themin your <EMCLASS="emphasis">.login</EM> file probably isn't wise.)</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11750"></A>Some people like to use aliases to redefine UNIX commands.For instance, you could make an alias named <EMCLASS="emphasis">rm</EM> that actuallyruns <EMCLASS="emphasis">mv</EM>, moving a file to a "trashcan" directory instead ofactually removing it.[1]Redefining commands can be<SPANCLASS="link">confusing or dangerous (<ACLASS="linkend"HREF="ch10_06.htm"TITLE="Avoiding C Shell Alias Loops ">10.6</A>)</SPAN>.Still, in some cases, aliases that redefine commands can be useful.</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[1] Article<ACLASS="xref"HREF="ch23_09.htm"TITLE="delete: Protecting Files from Accidental Deletion ">23.9</A>shows the <EMCLASS="emphasis">delete</EM> programs, a better way to do this.</P></BLOCKQUOTE><PCLASS="para">To temporarily use the default <EMCLASS="emphasis">rm</EM> (not your alias named <EMCLASS="emphasis">rm</EM>),type <SPANCLASS="link">a backslash (<CODECLASS="literal">\</CODE>) before the name (<ACLASS="linkend"HREF="ch08_12.htm"TITLE="Which One Will the C Shell Use? ">8.12</A>)</SPAN>:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>\rm</B></CODE> <CODECLASS="replaceable"><I>filename</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">To use the default <EMCLASS="emphasis">rm</EM> for the rest of your login session:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>unalias rm</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Unless you remove the definition from <EMCLASS="emphasis">.cshrc</EM>, the alias is restored the next time you log in(or the next time you create any new C shell).</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-11777"></A>A final piece of trivia: the C shell manual page tells us that aliasescan be nested; that is, they can refer to other aliases. Personally,I think this would get too complicated too quickly to be very useful,so I don't do it and can't recommend it. But you can try.</P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN>, <SPANCLASS="authorinitials">JP</SPAN>, <SPANCLASS="authorinitials">DG</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_01.htm"TITLE="10.1 Creating Custom Commands "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 10.1 Creating Custom 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_03.htm"TITLE="10.3 C Shell Aliases with Command-Line Arguments "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 10.3 C Shell Aliases with Command-Line Arguments "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">10.1 Creating Custom 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.3 C Shell Aliases with Command-Line Arguments </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 + -