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

📄 ch02_07.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 2] 2.7 C Shell Setup Files Aren't Read When You Want Them to Be? </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:32:04Z"><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="ch02_01.htm"TITLE="2. Logging In"><LINKREL="prev"HREF="ch02_06.htm"TITLE="2.6 Use Absolute Pathnames in Shell Setup Files "><LINKREL="next"HREF="ch02_08.htm"TITLE="2.8 Identifying Login Shells "></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="ch02_06.htm"TITLE="2.6 Use Absolute Pathnames in Shell Setup Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 2.6 Use Absolute Pathnames in Shell Setup Files "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 2<BR>Logging In</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch02_08.htm"TITLE="2.8 Identifying Login Shells "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 2.8 Identifying Login Shells "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1080">2.7 C Shell Setup Files Aren't Read When You Want Them to Be? </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-3161"></A><ACLASS="indexterm"NAME="AUTOID-3164"></A>The C shell reads its <EMCLASS="emphasis">.cshrc</EM>, <EMCLASS="emphasis">.login</EM>, and <EMCLASS="emphasis">.logout</EM> setupfiles<SPANCLASS="link">at particular times (<ACLASS="linkend"HREF="ch02_02.htm"TITLE="Shell Setup Files-Which, Where, and Why ">2.2</A>)</SPAN>.Only<SPANCLASS="link">&quot;login&quot; C shells (<ACLASS="linkend"HREF="ch02_08.htm"TITLE="Identifying Login Shells ">2.8</A>)</SPAN>will read the <EMCLASS="emphasis">.login</EM> and <EMCLASS="emphasis">.logout</EM> files.  Back when<EMCLASS="emphasis">csh</EM> was designed, this restriction worked fine.  The shell thatstarted as you logged in was flagged as a login shell, and it read allthree files.  You started other shells (shell escapes, shell scripts,etc.) from that login shell, and they would read only <EMCLASS="emphasis">.cshrc</EM>.</P><PCLASS="para">Now, UNIX has interactive shells started by window systems (like<SPANCLASS="link"><EMCLASS="emphasis">xterm</EM> (<ACLASS="linkend"HREF="ch01_31.htm"TITLE="The X Window System ">1.31</A>)</SPAN>),remote shells (like<SPANCLASS="link"><EMCLASS="emphasis">rsh</EM> (<ACLASS="linkend"HREF="ch01_33.htm"TITLE="UNIX Networking and Communications ">1.33</A>)</SPAN>),and other shells that might need some things set from the<EMCLASS="emphasis">.login</EM> or <EMCLASS="emphasis">.logout</EM> files.  Depending on how theseshells are invoked, these might not be login shells&nbsp;- so they mightread only <EMCLASS="emphasis">.cshrc</EM>.  How can you handle that?  Putting<ACLASS="indexterm"NAME="AUTOID-3184"></A><ACLASS="indexterm"NAME="AUTOID-3186"></A>all your setup commands in <EMCLASS="emphasis">.cshrc</EM> isn't good because all<SPANCLASS="link">subshells (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>read it... you definitely don't want to run terminal-setting commandslike <EMCLASS="emphasis">tset</EM> during shell escapes!</P><PCLASS="para">To handle problems at login time, put almost all of your setupcommands in <EMCLASS="emphasis">.cshrc</EM> instead of <EMCLASS="emphasis">.login</EM>.  After the&quot;login-only&quot; commands have been read from <EMCLASS="emphasis">.cshrc</EM>, set the<EMCLASS="emphasis">ENV_SET</EM><SPANCLASS="link">environment variable (<ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>)</SPAN>as a flag.(There's nothing special about this name.You can pick any name you want.)</P><PCLASS="para">The shell will copy the &quot;flag&quot; variable to subshells and the<EMCLASS="emphasis">.cshrc</EM> can test for it&nbsp;- if the variable exists, the login-onlycommands are skipped.  That'll keep the commands from being read againin a shell escape.</P><PCLASS="para">Here are parts of a <EMCLASS="emphasis">.cshrc</EM> that show the idea:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch47_03.htm"TITLE="47.3 Conditional Statements with if ">if</A> <ACLASS="co"HREF="ch47_04.htm"TITLE="47.4 C Shell Variable Operators and Expressions ">$?</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">...<EMCLASS="emphasis">Normal .cshrc stuff</EM>...if ($?prompt &amp;&amp; ! $?ENV_SET) then    # Do commands that used to go in .login file:    setenv EDITOR /usr/ucb/vi    tset        ...    setenv ENV_SET doneendif</PRE></TD></TR></TABLE></P><PCLASS="para"></P><PCLASS="para">You should put a comment in the <EMCLASS="emphasis">.login</EM> file to explainwhat you've done.</P><PCLASS="para">The <EMCLASS="emphasis">.logout</EM> file should probably be read only once&nbsp;- when your last(&quot;top-level&quot;) shell exits.If your top-level shell isn't a login shell, you can make it read <EMCLASS="emphasis">.logout</EM>anyway. Here's how.First, along with the previous fixes to your <EMCLASS="emphasis">.cshrc</EM> file, add an aliasthat will read your <EMCLASS="emphasis">.logout</EM> file when you use the <EMCLASS="emphasis">exit</EM> command.Also set the<SPANCLASS="link"><EMCLASS="emphasis">ignoreeof</EM> variable (<ACLASS="linkend"HREF="ch03_05.htm"TITLE="Stop Accidental C Shell Logouts ">3.5</A>)</SPAN>to force you to use the <EMCLASS="emphasis">exit</EM>command when you log out.Now the chunk of your <EMCLASS="emphasis">.cshrc</EM> will look like:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch10_06.htm"TITLE="10.6 Avoiding C Shell Alias Loops ">&quot;&quot;exit</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">if ($?prompt &amp;&amp; ! $?ENV_SET) then        ...    # Make all top-level interactive shells read .logout file:    set ignoreeof    alias exit 'source ~/.logout; &quot;&quot;exit'        ...endif</PRE></TD></TR></TABLE></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="ch02_06.htm"TITLE="2.6 Use Absolute Pathnames in Shell Setup Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 2.6 Use Absolute Pathnames in Shell Setup Files "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="ch02_08.htm"TITLE="2.8 Identifying Login Shells "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 2.8 Identifying Login Shells "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">2.6 Use Absolute Pathnames in Shell Setup Files </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">2.8 Identifying Login Shells </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 + -