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

📄 ch08_07.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 8] 8.7 Setting Your Search Path </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:34:16Z"><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="ch08_01.htm"TITLE="8. How the Shell Interprets What You Type"><LINKREL="prev"HREF="ch08_06.htm"TITLE="8.6 Output Command-Line Arguments "><LINKREL="next"HREF="ch08_08.htm"TITLE="8.8 A Directory for Commands You Shouldn't Run "></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="ch08_06.htm"TITLE="8.6 Output Command-Line Arguments "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.6 Output Command-Line Arguments "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 8<BR>How the Shell Interprets What You Type</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch08_08.htm"TITLE="8.8 A Directory for Commands You Shouldn't Run "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.8 A Directory for Commands You Shouldn't Run "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7864">8.7 Setting Your Search Path </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-7883"></A><ACLASS="indexterm"NAME="AUTOID-7886"></A>Your<SPANCLASS="link">search path (<ACLASS="linkend"HREF="ch06_04.htm"TITLE="The PATH Environment Variable ">6.4</A>, <ACLASS="linkend"HREF="ch06_05.htm"TITLE="PATH and path ">6.5</A>)</SPAN>controls what directories&nbsp;- and in what order&nbsp;- the shell searches for<SPANCLASS="link">external (<ACLASS="linkend"HREF="ch01_10.htm"TITLE="Internal and External Commands ">1.10</A>)</SPAN>commands.You can set a search path that takes effect every time you log inby editing your shell<SPANCLASS="link">setup file (<ACLASS="linkend"HREF="ch02_02.htm"TITLE="Shell Setup Files-Which, Where, and Why ">2.2</A>)</SPAN>.You might also want to change the path temporarily.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7864-SECT-1.1">8.7.1 Setting Path in Shell Setup Files </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-7895"></A>To change the &quot;default&quot; search path used every time you log in,edit the <CODECLASS="literal">PATH=...</CODE> line in your <EMCLASS="emphasis">.profile</EM> fileor the <CODECLASS="literal">set</CODE> <CODECLASS="literal">path=(...)</CODE> line in your <EMCLASS="emphasis">.cshrc</EM><ACLASS="indexterm"NAME="AUTOID-7903"></A><ACLASS="indexterm"NAME="AUTOID-7906"></A>or <EMCLASS="emphasis">.login</EM> file.</P><PCLASS="para">Add the<SPANCLASS="link">absolute pathname (<ACLASS="linkend"HREF="ch14_02.htm"TITLE="Using Relative and Absolute Pathnames ">14.2</A>)</SPAN>of the directory to the path.You have a choice:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">You can put the directory at the end of your path.(I think that's the best idea unless you know exactly what you're doing.)Then, commands in the directories you add will be used only if they haveuniquenamesthat aren't found anywhere else in the path.You can check that with a command like<SPANCLASS="link"><EMCLASS="emphasis">which</EM> (<ACLASS="linkend"HREF="ch50_08.htm"TITLE="Which Version Am I Using? ">50.8</A>)</SPAN>.</P></LI><LICLASS="listitem"><PCLASS="para">If you put the pathname close to the start of the path, beforestandard system directorieslike <EMCLASS="emphasis">/bin</EM>, then commands in the directory you addwill be used instead of system commands with the same name.That lets you replace commands that don't work the wayyou want with your own version.For instance, if you had the<SPANCLASS="link"><EMCLASS="emphasis">cal</EM> script that marks today's date (<ACLASS="linkend"HREF="ch48_07.htm"TITLE="cal That Marks Today's Date ">48.7</A>)</SPAN>in<SPANCLASS="link">your <EMCLASS="emphasis">bin</EM> (<ACLASS="linkend"HREF="ch04_02.htm"TITLE="A bin Directory for Your Programs and Scripts ">4.2</A>)</SPAN>,it would be used instead of the<SPANCLASS="link">system <EMCLASS="emphasis">cal</EM> (<ACLASS="linkend"HREF="ch48_06.htm"TITLE="Get Calendar for Any Month or Year: cal ">48.6</A>)</SPAN>.</P><PCLASS="para">If you set your path this way, you shouldbe especially careful not toaccidentally give some random program the same name as a system command&nbsp;- article<ACLASS="xref"HREF="ch44_21.htm"TITLE="Picking a Name for a New Command ">44.21</A>explains how to check for that.Also, be sure to make the directory unwritable by other users(with <CODECLASS="literal">chmod go-w</CODE>)-so they can't add malicious programs withthe same names as system utilities.</P></LI></UL><BLOCKQUOTECLASS="caution"><PCLASS="para"><STRONG>CAUTION:</STRONG> <ACLASS="indexterm"NAME="AUTOID-7931"></A>Installing your own version of standard system commands (like <EMCLASS="emphasis">ls</EM>or <EMCLASS="emphasis">rm</EM>) at the front of your path has a serious consequence.Many system programs and shell scripts will call a program like <EMCLASS="emphasis">ls</EM>and expect it to work just like the default system version of that program.If you install a version at the front of your search path that behavesdifferently, that can cause serious problems for an unsuspecting program.For example, you might install a version of <EMCLASS="emphasis">rm</EM> that writes messagesto standard output like &quot;Do you want to remove this file?&quot; and readsyour answer from standard input.The standard system <EMCLASS="emphasis">rm</EM> command won't prompt if its standard inputisn't a terminal.If your custom <EMCLASS="emphasis">rm</EM> doesn't work the same way as the system <EMCLASS="emphasis">rm</EM>,other programs that call <EMCLASS="emphasis">rm</EM> can mysteriously lock up whilethey wait (forever) for your private <EMCLASS="emphasis">rm</EM> to get an answer to its prompt.If you want to replace a system command, it's better to give yourversion a different name.</P></BLOCKQUOTE><PCLASS="para">When you log in, as your shell starts, before your setup files are read,your system probably has already set a default search path for you.Your system administrator can change that path.If your system has a default path, you should think about using it as part ofyour path&nbsp;- ask your administrator.To do that, include the variable <CODECLASS="literal">$PATH</CODE> or <CODECLASS="literal">$path</CODE> as you setyour path.For example, to add your <EMCLASS="emphasis">bin</EM> directory at the end of the systempath, use one of the following lines:[4]</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[4] There's a small problem with this if you set your path in your <EMCLASS="emphasis">.cshrc</EM>or <EMCLASS="emphasis">ksh ENV</EM> file.Each time you start a<SPANCLASS="link">subshell (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>,your <EMCLASS="emphasis">bin</EM> directory will be added to the path again.That won't cause any errors but it will make the path longer than itneeds to be.If you want to work around this, use an environment variable like<SPANCLASS="link"><EMCLASS="emphasis">ENV_SET</EM> (<ACLASS="linkend"HREF="ch02_07.htm"TITLE="C Shell Setup Files Aren't Read When You Want Them to Be? ">2.7</A>)</SPAN>as a flag&nbsp;- and set the path only if <EMCLASS="emphasis">ENV_SET</EM> isn't set.</P></BLOCKQUOTE><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">set path=($path ~/bin)                 <EMCLASS="emphasis">C shell</EM>PATH=$PATH:$HOME/bin                   <EMCLASS="emphasis">Bourne shell</EM></PRE></BLOCKQUOTE></P><PCLASS="para">&#13;For Bourne-type shells, load the updated <EMCLASS="emphasis">PATH</EM> by typing a command like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>. .profile</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">&#13;For the C shell, type one of these commands, depending on which file you changed:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>source .cshrc</B></CODE>% <CODECLASS="userinput"><B>source .login</B></CODE></PRE></BLOCKQUOTE></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-7864-SECT-1.2">8.7.2 Changing Path on the Command Line </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-7973"></A>As you work, you might need to add a directory to your path temporarily.For example, when I develop new versions of existing programs, I put themin a separate directory named something like <EMCLASS="emphasis">alpha-test</EM>.I don't usually want to run the alpha-test commands&nbsp;- but when I do, Iadd the <EMCLASS="emphasis">alpha-test</EM> directory to the front of my path temporarily.(It's handy to set the new path in a<SPANCLASS="link">subshell (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>so it won't change the path in my other shell.)Use the same path setting command you'd use in a shell setup file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>set path=(~/xxx/alpha-test $path)</B></CODE>      <EMCLASS="emphasis">C shell</EM>$ <CODECLASS="userinput"><B>PATH=$HOME/xxx/alpha-test:$PATH</B></CODE>        <EMCLASS="emphasis">Bourne shell</EM>$ <CODECLASS="userinput"><B>export PATH</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">&#13;Article<ACLASS="xref"HREF="ch08_08.htm"TITLE="A Directory for Commands You Shouldn't Run ">8.8</A>shows another way to change your path:command-by-command instead of directory-by-directory.</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="ch08_06.htm"TITLE="8.6 Output Command-Line Arguments "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 8.6 Output Command-Line Arguments "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="ch08_08.htm"TITLE="8.8 A Directory for Commands You Shouldn't Run "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 8.8 A Directory for Commands You Shouldn't Run "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">8.6 Output Command-Line Arguments </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">8.8 A Directory for Commands You Shouldn't Run </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 + -