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

📄 ch06_04.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 6] 6.4 The PATH Environment Variable </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-10-23T15:38:01Z"><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="ch06_01.htm"TITLE="6. Shell and Environment Variables"><LINKREL="prev"HREF="ch06_03.htm"TITLE="6.3 Predefined Environment Variables "><LINKREL="next"HREF="ch06_05.htm"TITLE="6.5 PATH and path "></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="ch06_03.htm"TITLE="6.3 Predefined Environment Variables "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 6.3 Predefined Environment Variables "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 6<BR>Shell and Environment Variables</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch06_05.htm"TITLE="6.5 PATH and path "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 6.5 PATH and path "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1172">6.4 The PATH Environment Variable </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-6012"></A>Of all the environment variables, the <EMCLASS="emphasis">PATH</EM> and <SPANCLASS="link"><EMCLASS="emphasis">TERM</EM> (<ACLASS="linkend"HREF="ch05_10.htm"TITLE="Finding What Terminal Names You Can Use ">5.10</A>)</SPAN>variables are the most important. The others are often greatconveniences; but <EMCLASS="emphasis">PATH</EM> and <EMCLASS="emphasis">TERM</EM> can make your life miserable if theyget screwed up.<ACLASS="indexterm"NAME="AUTOID-6019"></A></P><PCLASS="para">The <EMCLASS="emphasis">PATH</EM> variable is just a list of directories separated bycolon (<CODECLASS="literal">:</CODE>) characters.The shell searches through these directories in order whenever itneeds to find a command. So, if you want to execute commands in<EMCLASS="emphasis">/bin</EM>, <EMCLASS="emphasis">/usr/bin</EM>, <EMCLASS="emphasis">/usr/local</EM>, the current directory,and your personal <EMCLASS="emphasis">bin</EM> directory, you would put aline like the one below in your <EMCLASS="emphasis">.login</EM> file.An empty entry (<CODECLASS="literal">:</CODE> as the first or last character,or <CODECLASS="literal">::</CODE> in the middle) means &quot;the current directory.&quot;</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch04_02.htm"TITLE="4.2 A bin Directory for Your Programs and Scripts ">$HOME/bin</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">setenv PATH /bin:/usr/bin:/usr/local::$HOME/bin</PRE></TD></TR></TABLE></P><PCLASS="para">Article<ACLASS="xref"HREF="ch08_07.htm"TITLE="Setting Your Search Path ">8.7</A>explains more about setting the path.</P><PCLASS="para">The most common problem with <EMCLASS="emphasis">PATH</EM> is that, somehow, it gets deleted.This usually happens if you try to change <EMCLASS="emphasis">PATH</EM> and do so incorrectly.When <EMCLASS="emphasis">PATH</EM> is deleted, your shell can only find its <SPANCLASS="link">built-in commands (<ACLASS="linkend"HREF="ch01_10.htm"TITLE="Internal and External Commands ">1.10</A>)</SPAN>and commands for which you give the complete pathname.Here's ademonstration:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>setenv PATH   </B></CODE><EMCLASS="emphasis">Set PATH to null accidentally</EM>% <CODECLASS="userinput"><B>ls</B></CODE>ls: Command not found.&#13;</PRE></BLOCKQUOTE></P><PCLASS="para">Needless to say, this can be very frustrating&nbsp;- especially if you can'tfigure out what's going on. There are a couple of easy fixes. Theeasiest is just to log out andlog back in again. (<EMCLASS="emphasis">logout</EM> is a built-in C shell command, soyou won't have trouble finding it. If you get an error message like&quot;Not login shell,&quot; try <EMCLASS="emphasis">exit</EM> instead.)Anotherfix is to<SPANCLASS="link">read (<ACLASS="linkend"HREF="ch44_23.htm"TITLE="Reading Files with the . and source Commands ">44.23</A>)</SPAN>whicheverinitialization file defined your <EMCLASS="emphasis">PATH</EM> variable, usually <EMCLASS="emphasis">.login</EM>for C shell users or <EMCLASS="emphasis">.profile</EM> for Bourne shell users:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>source ~/.login</B></CODE>$ <CODECLASS="userinput"><B>. $HOME/.profile</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">This will almost certainly give you <EMCLASS="emphasis">some</EM> of your path back; theproblem is that a lot of initialization files merely add a few &quot;private&quot;directories to a system-wide default path. In this case, just executethe system-wide initialization files first (if your system has them).Their pathnames vary:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>source /usr/lib/Cshrc</B></CODE>% <CODECLASS="userinput"><B>source /usr/lib/Login</B></CODE>% <CODECLASS="userinput"><B>source ~/.login</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">The other common <EMCLASS="emphasis">PATH</EM> problem is that users sometimes can't find thecommands they want. This happens most often when someone writes a newshell script with the same name as a standard UNIX command&nbsp;- say,<EMCLASS="emphasis">true</EM>. He or she tries to execute it and can't; in fact, allthat happens is:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>true</B></CODE>%</PRE></BLOCKQUOTE></P><PCLASS="para">After staring at the script for a long time, the user sometimes getsthe right idea: the script is fine, it's the path that's wrong. The<EMCLASS="emphasis">PATH</EM> variable will look something like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>printenv PATH</B></CODE>/bin:/usr/local:/usr/ucb:/usr/bin::/home/mkl/bin</PRE></BLOCKQUOTE></P><PCLASS="para">The shell searches the <EMCLASS="emphasis">PATH</EM> in order; therefore, it finds the system'sstandard <EMCLASS="emphasis">true</EM> command before seeing the new one. The newcommand never gets a chance.You <EMCLASS="emphasis">could</EM> fix this problem by putting the current directory and<EMCLASS="emphasis">$HOME/bin</EM> at the head of the search path, in which case,commands in the current directory and your private <EMCLASS="emphasis">bin</EM> directory willoverride the standard commands. However, that's <EMCLASS="emphasis">not</EM>recommended; it's a well-known security hole.</P><PCLASS="para">So what is recommended? Nothing much, except: if you write shellscripts or other programs, give them names that are<SPANCLASS="link">different from the standard UNIX utilities (<ACLASS="linkend"HREF="ch44_21.htm"TITLE="Picking a Name for a New Command ">44.21</A>)</SPAN>.If you really need an overlapping name, you can use a<SPANCLASS="link">relative pathname (<ACLASS="linkend"HREF="ch01_21.htm"TITLE="Making Pathnames ">1.21</A>)</SPAN>to specify &quot;the program called <EMCLASS="emphasis">true</EM> in the current directory&quot;:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>./true</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Here are some related articles.You can search your <EMCLASS="emphasis">PATH</EM> for a command with<SPANCLASS="link"><EMCLASS="emphasis">which</EM> (<ACLASS="linkend"HREF="ch50_08.htm"TITLE="Which Version Am I Using? ">50.8</A>)</SPAN>,<SPANCLASS="link"><EMCLASS="emphasis">findcmd</EM> (<ACLASS="linkend"HREF="ch16_10.htm"TITLE="findcmd: Find a Command in Your Search Path ">16.10</A>)</SPAN>,and<SPANCLASS="link"><EMCLASS="emphasis">whereiz</EM> (<ACLASS="linkend"HREF="ch04_10.htm"TITLE="Find All Command Versions with whereiz ">4.10</A>)</SPAN>.Article<ACLASS="xref"HREF="ch06_05.htm"TITLE="PATH and path ">6.5</A>explains the C shell's <EMCLASS="emphasis">path</EM> variable.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</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="ch06_03.htm"TITLE="6.3 Predefined Environment Variables "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 6.3 Predefined Environment Variables "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="ch06_05.htm"TITLE="6.5 PATH and path "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 6.5 PATH and path "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">6.3 Predefined Environment Variables </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">6.5 PATH and path </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 + -