📄 ch10_06.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 10] 10.6 Avoiding C Shell Alias Loops </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:35:53Z"><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_05.htm"TITLE="10.5 Sourceable Scripts "><LINKREL="next"HREF="ch10_07.htm"TITLE="10.7 How to Put if-then-else in a C Shell Alias "></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_05.htm"TITLE="10.5 Sourceable Scripts "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 10.5 Sourceable Scripts "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_07.htm"TITLE="10.7 How to Put if-then-else in a C Shell Alias "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 10.7 How to Put if-then-else in a C Shell Alias "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0253">10.6 Avoiding C Shell Alias Loops </A></H2><PCLASS="para">[Article<ACLASS="xref"HREF="ch08_11.htm"TITLE="Which One Will bash Use? ">8.11</A>has similar information for <ICLASS="filename">bash</I>. -JP]</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-12008"></A><ACLASS="indexterm"NAME="AUTOID-12011"></A><ACLASS="indexterm"NAME="AUTOID-12013"></A><ACLASS="indexterm"NAME="AUTOID-12016"></A>Here's a situation that came up on the Net a while ago. Someonewanted an<SPANCLASS="link"><EMCLASS="emphasis">exit</EM> (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>alias that would run a<SPANCLASS="link"><EMCLASS="emphasis">~/.exit</EM> file (<ACLASS="linkend"HREF="ch14_14.htm"TITLE="Automatic Setup When You Enter/Exit a Directory ">14.14</A>)</SPAN>beforeleaving the shell. The obvious solution is:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias exit "source ~/.exit; exit"</PRE></BLOCKQUOTE></P><PCLASS="para">This doesn't work; when you use the <EMCLASS="emphasis">exit</EM> alias, the C shellthinks that the alias is trying to execute itself.Recursive aliases aren't allowed on many shells,so the C shell prints an error message (<CODECLASS="literal">Alias loop</CODE>) andgives up.</P><PCLASS="para">There are many, many ways to break the loop. Here's the best (in myopinion):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias exit 'source ~/.exit; ""exit'</PRE></BLOCKQUOTE></P><PCLASS="para">Article<ACLASS="xref"HREF="ch08_12.htm"TITLE="Which One Will the C Shell Use? ">8.12</A>has the hairy details of what works and why.To summarize, if you need to use the alias's name within a C shellalias, you can use:<TABLECLASS="informaltable"><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">""<CODECLASS="replaceable"><I>name</I></CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">where <CODECLASS="replaceable"><I>name</I></CODE> is the name of a<SPANCLASS="link">built-in (<ACLASS="linkend"HREF="ch01_10.htm"TITLE="Internal and External Commands ">1.10</A>)</SPAN>commandor any "regular" command.</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1">\<CODECLASS="replaceable"><I>name</I></CODE></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">where <CODECLASS="replaceable"><I>name</I></CODE> is the name ofany "regular" command, but not a built-in command.</P></TD></TR></TBODY></TABLE> </P><PCLASS="para">Tempting as this all may sound (and I have to admit, if it didn'tsound a bit tempting, I wouldn't be writing this article), I can'treally recommend the practice of "redefining" commands with aliases. You shouldleave the original commands as they are. The original author couldhave avoided all these problems by calling his alias <EMCLASS="emphasis">quit</EM> ratherthan <EMCLASS="emphasis">exit</EM>.</P><PCLASS="para">If you redefine commands with aliases - then use another accountwhere your alias isn't defined (or, if you let someone type a commandon your account)-it's easy for things to go wrong.That's especially true for commands that do somethingpermanent - overwriting or removing files, for example.</P><PCLASS="para">Let me give one more example to show you what problems you can have. Let's say you've aliased the <EMCLASS="emphasis">exit</EM> command to <EMCLASS="emphasis">source</EM> a<EMCLASS="emphasis">.exit</EM> file before quitting. Fair enough. But now, let'ssay that you're not in your login shell, that you've <SPANCLASS="link"><EMCLASS="emphasis">set ignoreeof</EM> (<ACLASS="linkend"HREF="ch06_09.htm"TITLE="Special C Shell Variables ">6.9</A>)</SPAN>,and that, for no apparent reason, your <EMCLASS="emphasis">.exit</EM>file disappears (maybe it develops a bad block, so the system can'tread it; such things happen).</P><PCLASS="para">Now you're stuck. If you type <CODECLASS="literal">exit</CODE>, the <EMCLASS="emphasis">source</EM> commandwill fail, and the "real" <EMCLASS="emphasis">exit</EM> command will never be executed.You can't leave the shell. Of course, if you remember what you did,you can always type <CODECLASS="literal">unalias exit</CODE> and get the original command back.Or you can type <CODECLASS="literal"><"><">exit</CODE>.But if you've foisted this alias on a beginner, he or she mightnot know that. All of a sudden, you're stuck in some shell that youapparently can't get out of.</P><PCLASS="para">The biggest virtue of UNIX is that it's infinitely extendable.However, you aren't helping if your extensions hide the basicoperations that make everything work. So - extend all you want. Butwhen you write your extensions, give them <EMCLASS="emphasis">new names</EM>.End of sermon.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN> <SPANCLASS="bibliomisc">ML wrote, but material came from net postings by several people</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_05.htm"TITLE="10.5 Sourceable Scripts "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 10.5 Sourceable Scripts "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_07.htm"TITLE="10.7 How to Put if-then-else in a C Shell Alias "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 10.7 How to Put if-then-else in a C Shell Alias "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">10.5 Sourceable Scripts </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.7 How to Put if-then-else in a C Shell Alias </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 + -