📄 ch02_05.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 2] 2.5 Tips for Speeding up Slow Logins </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:31:58Z"><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_04.htm"TITLE="2.4 Tip for Changing Account Setup: Keep a Shell Ready "><LINKREL="next"HREF="ch02_06.htm"TITLE="2.6 Use Absolute Pathnames in Shell Setup Files "></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_04.htm"TITLE="2.4 Tip for Changing Account Setup: Keep a Shell Ready "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 2.4 Tip for Changing Account Setup: Keep a Shell Ready "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_06.htm"TITLE="2.6 Use Absolute Pathnames in Shell Setup Files "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 2.6 Use Absolute Pathnames in Shell Setup Files "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-5740">2.5 Tips for Speeding up Slow Logins </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-2992"></A><ACLASS="indexterm"NAME="AUTOID-2995"></A>When I first started using the C shell in the early 1980s, I madeincredible<SPANCLASS="link"><EMCLASS="emphasis">.cshrc</EM> and <EMCLASS="emphasis">.login</EM> files (<ACLASS="linkend"HREF="ch02_02.htm"TITLE="Shell Setup Files-Which, Where, and Why ">2.2</A>)</SPAN>with all kinds of nice customizations. Aliases, commands to check mymail, calendar systems, shell scripts in the background to watchthings for me... boy, was this great! Except when I tried to log in,that is. I was working on an overloaded VAX 11/750. Logging in couldtake a few minutes, from when I got the <CODECLASS="literal">;login:</CODE> promptuntil I finally got my shell prompt <CODECLASS="literal">%</CODE> (...well, it was reallya much fancier prompt, but that's another story <CODECLASS="literal">:-)</CODE>).</P><PCLASS="para">The C shell seems (to me) to be pretty slow at reading long<EMCLASS="emphasis">.cshrc</EM> and <EMCLASS="emphasis">.login</EM> files - especially at settingaliases. So, I learned some ways to get logged in faster. They wereespecially nice when I was at someone else's terminal and needed tolog in for something quick. You might not want to use these exacttechniques, but I hope they'll give you some ideas if your logins taketoo long. The same ideas will work on other shells - but with thatshell's commands and syntax, of course.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-5740-SECT-1.1">2.5.1 Quick Login </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-3010"></A>Add a "quick login" setup to the top of your <EMCLASS="emphasis">.cshrc</EM>.As soon as the C shell starts and sets a few essentials, this setupasks whether you want a prompt right away.<ACLASS="indexterm"NAME="AUTOID-3013"></A>If you answer yes, it starts another C shell with the <EMCLASS="emphasis">-f</EM> option(important: this makes the<SPANCLASS="link">subshell (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>skip your <EMCLASS="emphasis">.cshrc</EM> so you don'tget a loop):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">login: <CODECLASS="userinput"><B>jerry</B></CODE>Password:Last login: Tue Jan 21 12:34:56 PST 1985 ...Answer y for quick login or RETURN for standard: <CODECLASS="userinput"><B>y</B></CODE>For a standard login, type 'exit 77'.% <CODECLASS="userinput"><B>mail bigboss</B></CODE>Subject: <CODECLASS="userinput"><B>I'm on my wayCarol, I'm leaving for the meeting now. See you by 10:00..</B></CODE>% <KBDCLASS="keycap">[CTRL-d]</KBD>login:</PRE></BLOCKQUOTE></P><PCLASS="para">From there, I can run a few quick commands.<ACLASS="indexterm"NAME="AUTOID-3027"></A>Typing CTRL-d or <CODECLASS="literal">exit</CODE> quits the quick subshell and kills my originallogin shell, too.If I want to stay logged in on that terminal, I type <CODECLASS="literal">exit</CODE> <CODECLASS="literal">77</CODE>.That makes the quick subshell return an<SPANCLASS="link">exit status (<ACLASS="linkend"HREF="ch44_07.htm"TITLE="Exit Status of UNIX Processes ">44.7</A>)</SPAN>of 77;<ACLASS="indexterm"NAME="AUTOID-3033"></A><ACLASS="indexterm"NAME="AUTOID-3035"></A>the test in the <EMCLASS="emphasis">.cshrc</EM> notices this and continues logging me in,reading the rest of the <EMCLASS="emphasis">.cshrc</EM> and <EMCLASS="emphasis">.login</EM>.</P><PCLASS="para">Here's the top of the <EMCLASS="emphasis">.cshrc</EM> file to set that up:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <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> <ACLASS="co"HREF="ch09_05.htm"TITLE="9.5 Build Strings with { } ">{ }</A> <ACLASS="co"HREF="ch47_04.htm"TITLE="47.4 C Shell Variable Operators and Expressions ">$< =~</A> <ACLASS="co"HREF="ch38_10.htm"TITLE="38.10 Destroying Processes with kill ">kill</A> <ACLASS="co"HREF="ch08_19.htm"TITLE='8.19 "Special" Characters and Operators '>$$</A> <ACLASS="co"HREF="ch06_01.htm"TITLE="6.1 What Environment Variables Are Good For ">setenv</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"># only do stuff below if this is an interactive shellif (! $?prompt) goto cshrc_end# QUICK LOGIN:if (! $?LOGGEDIN) then set path = (/bin /usr/ucb /usr/local/{bin,mh} {/usr,~}/bin .) echo -n "Answer y for quick login or RETURN for standard: " if ("$<" =~ y*) then echo "For a standard login, type 'exit 77'." csh -f # PLAIN "exit" JUST EXITS .cshrc... THIS IS BRUTAL BUT IT WORKS: if ($status != 77) kill -9 $$ endifendifsetenv LOGGEDIN yes ...<EMCLASS="emphasis">Rest of .cshrc</EM>...cshrc_end:</PRE></TD></TR></TABLE></P><BLOCKQUOTECLASS="caution"><PCLASS="para"><STRONG>CAUTION:</STRONG> <ACLASS="indexterm"NAME="AUTOID-3056"></A><ACLASS="indexterm"NAME="AUTOID-3058"></A>Be sure to use an<SPANCLASS="link"><CODECLASS="literal">if</CODE> <CODECLASS="literal">($?prompt)</CODE> test (<ACLASS="linkend"HREF="ch02_09.htm"TITLE="Speeding Up Your C Shell with set prompt Test ">2.9</A>)</SPAN>firstto keep this command from being read by noninteractive shells.If you don't, non-interactive shells for jobs like <EMCLASS="emphasis">at</EM> may hang,waiting for an answer to the "quick login" question - or just be confusedand not work.</P></BLOCKQUOTE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-5740-SECT-1.2">2.5.2 A Second Alias and Command File </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-3067"></A>Maybe you have a set of aliases or setup commands that you use only forcertain projects.If you don't need that setup every time you log in, you can put thesetup commands in a separate file.<ACLASS="indexterm"NAME="AUTOID-3070"></A>Make an alias named something like <EMCLASS="emphasis">setup</EM> that reads the file into yoursetup shell.Only type <CODECLASS="literal">setup</CODE> when you need the extra setup done.</P><PCLASS="para">Here's the alias:</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> <ACLASS="co"HREF="ch44_23.htm"TITLE="44.23 Reading Files with the . and source Commands ">source</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">alias setup 'if (! $?setup) source ~/lib/cshrc2'</PRE></TD></TR></TABLE></P><PCLASS="para">and the start of the <EMCLASS="emphasis">~/lib/cshrc2</EM> file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">set setup # variable to stop re-sourcingalias foo bar ...</PRE></BLOCKQUOTE></P><PCLASS="para">The first line in the <EMCLASS="emphasis">cshrc2</EM> file sets a shell variable thatkeeps the <EMCLASS="emphasis">setup</EM> alias from re-reading the file into this shell.This saves time if you forget that you've already run <EMCLASS="emphasis">setup</EM>.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-5740-SECT-1.3">2.5.3 Once-a-Day Setup </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-3090"></A>Maybe there are some commands that you want to run only once a day,the first time you log in.For example, I had a reminder system that showed my calendar for the day,reminded me of birthdays, etc.A test like this in <EMCLASS="emphasis">.login</EM> handles that: </P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch47_05.htm"TITLE="47.5 Using C Shell Arrays ">$date[n]</A> <ACLASS="co"HREF="ch06_08.htm"TITLE="6.8 Shell Variables ">set</A> <ACLASS="co"HREF="ch09_16.htm"TITLE="9.16 Command Substitution ">`...`</A> <ACLASS="co"HREF="ch47_04.htm"TITLE="47.4 C Shell Variable Operators and Expressions ">-e</A> <ACLASS="co"HREF="ch21_07.htm"TITLE="21.7 Setting File Modification Time with touch ">touch</A> <ACLASS="co"HREF="ch06_08.htm"TITLE="6.8 Shell Variables ">unset</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"># Put day name in $date[1], month in $date[2], date in $date[3], etc:set date=(`date`)# if today's daily setup file doesn't exist, make it and do stuff:if (! -e ~/tmp/,setup.$date[3]) then touch ~/tmp/,setup.$date[3] do_calendar ...<EMCLASS="emphasis">Other once-a-day setup</EM>...endifunset date</PRE></TD></TR></TABLE></P><PCLASS="para">That test uses<SPANCLASS="link"><EMCLASS="emphasis">csh</EM> arrays (<ACLASS="linkend"HREF="ch47_05.htm"TITLE="Using C Shell Arrays ">47.5</A>)</SPAN>to get today's date and makean empty file in my <EMCLASS="emphasis">tmp</EM> directory with a name like <EMCLASS="emphasis">,setup.23</EM>.Once a file is created (say, on June 23), then the setup commands won't runagain that day.I have a program that<SPANCLASS="link"> periodically removes files named with a comma(<CODECLASS="literal">,</CODE>) (<ACLASS="linkend"HREF="ch23_20.htm"TITLE="Deleting Stale Files ">23.20</A>, <ACLASS="linkend"HREF="ch23_22.htm"TITLE="Using find to Clear Out Unneeded Files ">23.22</A>)</SPAN>so <EMCLASS="emphasis">,setup.23</EM> will be long gone by the next month's twenty-third day.That could also be done from the<SPANCLASS="link"><EMCLASS="emphasis">.logout</EM> file (<ACLASS="linkend"HREF="ch03_01.htm#UPT-ART-4800"TITLE="Running Commands When You Log Out ">3.1</A>, <ACLASS="linkend"HREF="ch03_02.htm"TITLE="Running Commands at Bourne/Korn Shell Logout ">3.2</A>)</SPAN>.</P></DIV><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_04.htm"TITLE="2.4 Tip for Changing Account Setup: Keep a Shell Ready "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 2.4 Tip for Changing Account Setup: Keep a Shell Ready "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_06.htm"TITLE="2.6 Use Absolute Pathnames in Shell Setup Files "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 2.6 Use Absolute Pathnames in Shell Setup Files "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">2.4 Tip for Changing Account Setup: Keep a Shell Ready </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.6 Use Absolute Pathnames in Shell Setup Files </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 + -