📄 ch14_10.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 14] 14.10 Variables Help You Find Directories and Files </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:37:08Z"><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="ch14_01.htm"TITLE="14. Moving Around in a Hurry"><LINKREL="prev"HREF="ch14_09.htm"TITLE="14.9 cd by Directory Initials "><LINKREL="next"HREF="ch14_11.htm"TITLE="14.11 Finding (Anyone's) Home Directory, Quickly "></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="ch14_09.htm"TITLE="14.9 cd by Directory Initials "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.9 cd by Directory Initials "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 14<BR>Moving Around in a Hurry</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch14_11.htm"TITLE="14.11 Finding (Anyone's) Home Directory, Quickly "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.11 Finding (Anyone's) Home Directory, Quickly "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1420">14.10 Variables Help You Find Directories and Files </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-15564"></A><ACLASS="indexterm"NAME="AUTOID-15567"></A><ACLASS="indexterm"NAME="AUTOID-15570"></A><ACLASS="indexterm"NAME="AUTOID-15573"></A><ACLASS="indexterm"NAME="AUTOID-15576"></A>A UNIX system can have hundreds or thousands of directories - and a lotmore files.Even if you remember all the pathnames, typing them over and over can be a pain.</P><PCLASS="para">Your account probably already has some helpful<ACLASS="indexterm"NAME="AUTOID-15580"></A><ACLASS="indexterm"NAME="AUTOID-15582"></A><SPANCLASS="link">shell and environment variables (<ACLASS="linkend"HREF="ch06_08.htm"TITLE="Shell Variables ">6.8</A>, <ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>)</SPAN>set up.You can add more from the command line or from your<SPANCLASS="link">shell setup files (<ACLASS="linkend"HREF="ch02_02.htm"TITLE="Shell Setup Files-Which, Where, and Why ">2.2</A>)</SPAN>(like <EMCLASS="emphasis">.cshrc</EM> or <EMCLASS="emphasis">.profile</EM>).<ACLASS="indexterm"NAME="AUTOID-15588"></A>To see what environment variables are set, use the <EMCLASS="emphasis">env</EM> (System V) or<ACLASS="indexterm"NAME="AUTOID-15591"></A><ACLASS="indexterm"NAME="AUTOID-15593"></A><EMCLASS="emphasis">printenv</EM> (Berkeley) command.The command <EMCLASS="emphasis">set</EM> should show shell variables (some of these mightbe repeated in the environment).Here's part of what happens on my account:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>env</B></CODE>HOME=/home/jpeekMH=/work/jpeek_mail/.mh_profilePATH=/home/jpeek/.bin:/home/jpeek/.bin/show:/work/bin:...RNINIT=/home/jpeek/.rnswitchesPAGER=/usr/local/bin/less% <CODECLASS="userinput"><B>set</B></CODE>active /usr/lib/news/activecwd /home/jpeek/pwrtoolsmail (60 /usr/mail/jpeek)maillog /usr/spool/smail/log/logfile</PRE></BLOCKQUOTE></P><PCLASS="para">UNIX programs use a lot of those environment variables.For instance, my email system finds its setup file from <EMCLASS="emphasis">MH</EM>.But I can use environment variables for other things, too.For instance, when I want to edit my email setup file, I can type<CODECLASS="literal">vi $MH</CODE> from any directory.The shell expands <CODECLASS="literal">$MH</CODE> to <EMCLASS="emphasis">/work/jpeek_mail/.mh_profile</EM> and startsthe editor.Check your environment and see what you've got; the names usuallyexplain the variables pretty well.</P><PCLASS="para">The shell uses shell variables like <CODECLASS="literal">$mail</CODE>.I can check incoming messages with the command<SPANCLASS="link"><CODECLASS="literal">tail $mail[2]</CODE> (<ACLASS="linkend"HREF="ch25_14.htm"TITLE="How to Look at the End of a File: tail ">25.14</A>, <ACLASS="linkend"HREF="ch47_05.htm"TITLE="Using C Shell Arrays ">47.5</A>)</SPAN>(the<CODECLASS="literal">[2]</CODE> tells the C shell to pick the second word from the list in<CODECLASS="literal">$mail</CODE>).</P><PCLASS="para">I've set other shell variables for myself.When I send some mail messages, I want to watch the system mail log tosee the message being delivered.I just type:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch25_16.htm"TITLE="25.16 How to Look at a File as It Grows ">-f</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>tail -f $maillog</B></CODE> ...09/08/96 17:13:27: [m0kJN4x-0000AKC] new msg: from jpeek@jpeek.com09/08/96 17:13:28: [m0kJN4x-0000AKC] <jim> ... delivered09/08/96 17:13:42: [m0kJN4x-0000AKC] <allan@comex.com> ... delivered</PRE></TD></TR></TABLE></P><PCLASS="para">Are there files or directories that you refer to a lot - ones that aren'tright for the<SPANCLASS="link"><EMCLASS="emphasis">cdpath</EM> (<ACLASS="linkend"HREF="ch14_05.htm"TITLE="Saving Time When You Change Directories: cdpath ">14.5</A>)</SPAN>or a shell alias?Pick a likely shell variable name and add the variable to your<EMCLASS="emphasis">.cshrc</EM> or <EMCLASS="emphasis">.profile</EM>.You can store more than one pathname in the same variable - either byseparating them with spaces or by using wildcards:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch08_06.htm"TITLE="8.6 Output Command-Line Arguments ">echo</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"># C shell variables:set worklog=~/todays_worklog <EMCLASS="emphasis">Single file, defined when set</EM>set projfiles=(/work/beta/data_3.9*) <EMCLASS="emphasis">Many files, defined when set</EM>set currdata='/work/beta/data_5*' <EMCLASS="emphasis">Many files, defined when used</EM># Bourne shell variables:worklog=$HOME/todays_worklog <EMCLASS="emphasis">Single file, defined when set</EM>projfiles="`echo /work/beta/data_3.9_*`" <EMCLASS="emphasis">Many files, defined when set</EM>currdata='/work/beta/data_5*' <EMCLASS="emphasis">Many files, defined when used</EM></PRE></TD></TR></TABLE></P><PCLASS="para">Then:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">You could type <CODECLASS="literal">vi + $worklog</CODE> any time you want to add a note to the endof the file <EMCLASS="emphasis">todays_worklog</EM> in your home directory.(The <CODECLASS="literal">+</CODE> tells <EMCLASS="emphasis">vi</EM> to start at the end of the file.)</P></LI><LICLASS="listitem"><PCLASS="para">The shell expands the asterisk (<CODECLASS="literal">*</CODE>) when it sets the <EMCLASS="emphasis">projfiles</EM>variable and stores a list of the files <EMCLASS="emphasis">as they were when thevariable was set</EM>.(If the list of files changes, it'll be reset when you start your next shell.)You could print all those files any time you wanted to by typing acommand like <CODECLASS="literal">lpr $projfiles</CODE>.The C shell also lets you<SPANCLASS="link">pick individual files (<ACLASS="linkend"HREF="ch47_05.htm"TITLE="Using C Shell Arrays ">47.5</A>)</SPAN>from the list - forinstance, <CODECLASS="literal">lpr $projfiles[9]</CODE> would print the ninth file from the list.</P></LI><LICLASS="listitem"><PCLASS="para">When the <EMCLASS="emphasis">currdata</EM> variable is set, the single quotes (<CODECLASS="literal">"</CODE>)around it<SPANCLASS="link">prevent expansion (<ACLASS="linkend"HREF="ch08_14.htm"TITLE="Bourne Shell Quoting ">8.14</A>)</SPAN>of the wildcard (<CODECLASS="literal">*</CODE>).Instead, the pathname <CODECLASS="literal">/work/beta/data_5*</CODE> is expanded when you usethe variable - like <CODECLASS="literal">pg $currdata</CODE> - to showyou the files as theyare at the time you use the variable.</P></LI></UL><PCLASS="para">You can also use variables to store the paths to directories.Use <EMCLASS="emphasis">cd</EM>, <EMCLASS="emphasis">ls</EM>, or any other command with the variables.</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="ch14_09.htm"TITLE="14.9 cd by Directory Initials "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.9 cd by Directory Initials "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="ch14_11.htm"TITLE="14.11 Finding (Anyone's) Home Directory, Quickly "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.11 Finding (Anyone's) Home Directory, Quickly "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">14.9 cd by Directory Initials </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">14.11 Finding (Anyone's) Home Directory, Quickly </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 + -