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

📄 ch50_08.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 50] 50.8 Which Version Am I Using? </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:56: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="ch50_01.htm"TITLE="50. Help--Online Documentation, etc."><LINKREL="prev"HREF="ch50_07.htm"TITLE="50.7 How UNIX Systems Remember Their Name "><LINKREL="next"HREF="ch50_09.htm"TITLE="50.9 Reading a Permuted Index "></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="ch50_07.htm"TITLE="50.7 How UNIX Systems Remember Their Name "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 50.7 How UNIX Systems Remember Their Name "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 50<BR>Help--Online Documentation, etc.</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch50_09.htm"TITLE="50.9 Reading a Permuted Index "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 50.9 Reading a Permuted Index "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-6740">50.8 Which Version Am I Using? </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-59354"></A><ACLASS="indexterm"NAME="AUTOID-59357"></A><ACLASS="indexterm"NAME="AUTOID-59359"></A><ACLASS="indexterm"NAME="AUTOID-59362"></A><ACLASS="indexterm"NAME="AUTOID-59365"></A><ACLASS="indexterm"NAME="AUTOID-59368"></A><ACLASS="indexterm"NAME="AUTOID-59371"></A><ACLASS="indexterm"NAME="AUTOID-59374"></A><ACLASS="indexterm"NAME="AUTOID-59377"></A>The <EMCLASS="emphasis">which</EM> command is a real life saver.It has becomeincreasingly important in the lastfew years.Many vendors (like Sun) are providing separate directoriesof BSD-compatible and System V-compatible commands.Which commandyou'll get depends on your <SPANCLASS="link"><EMCLASS="emphasis">PATH</EM> (<ACLASS="linkend"HREF="ch06_04.htm"TITLE="The PATH Environment Variable ">6.4</A>)</SPAN>environment variable.It's often essential to know which version you're using.For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>which sort</B></CODE>/bin/sort</PRE></BLOCKQUOTE></P><PCLASS="para">tells me exactly which version of the <EMCLASS="emphasis">sort</EM> program I'm using.(Under SunOS 4.1, this is the BSD-compatible version in <EMCLASS="emphasis">/bin</EM>,not the System V version in <EMCLASS="emphasis">/usr/5bin</EM>.)</P><PCLASS="para">You'll find that <EMCLASS="emphasis">which</EM> comes in handy in lots of othersituations.I find that I'm always using <EMCLASS="emphasis">which</EM> inside ofbackquotes to get a precise path.For example, when I was writing these articles, I startedwondering whether or not <EMCLASS="emphasis">man</EM>, <EMCLASS="emphasis">apropos</EM>, and <EMCLASS="emphasis">whatis</EM>were really the same executable.It's a simple question, but one Ihad never bothered to think about.There's one good way to find out:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>ls -li `which man` `which apropos` `which whatis`</B></CODE>102352 -rwxr-xr-x  3 root        24576 Feb  8  1990 /usr/ucb/apropos102352 -rwxr-xr-x  3 root        24576 Feb  8  1990 /usr/ucb/man102352 -rwxr-xr-x  3 root        24576 Feb  8  1990 /usr/ucb/whatis</PRE></BLOCKQUOTE></P><PCLASS="para">What does this tell us?First, the three commands have the same filesize, which means that they're likely to be identical; furthermore,each file has three links, meaning that each file has three names.The <EMCLASS="emphasis">-i</EM> option confirms it; all three files have the same<SPANCLASS="link">i-number (<ACLASS="linkend"HREF="ch01_22.htm"TITLE="How UNIX Keeps Track of Files: Inodes ">1.22</A>)</SPAN>.So, <EMCLASS="emphasis">apropos</EM>, <EMCLASS="emphasis">man</EM>, and <EMCLASS="emphasis">whatis</EM> are justone executable file that has three hard links.</P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="which">which</A><BR></TH><TDVALIGN="TOP">A few System V implementations don't have a <EMCLASS="emphasis">which</EM> command.The version of <EMCLASS="emphasis">which</EM> on the CD-ROM is even betterthan the BSD <EMCLASS="emphasis">which</EM>, anyway.By default, this new <EMCLASS="emphasis">which</EM> works about the same as the BSD <EMCLASS="emphasis">which</EM>.The new <EMCLASS="emphasis">which</EM> has a big plus: it doesn't try to read your <EMCLASS="emphasis">.cshrc</EM>file to see what aliases you've set there.Instead, you set it up with its <EMCLASS="emphasis">-\i</EM> optionto read your shell's <EMCLASS="emphasis">current</EM> list of aliases.This lets the new <EMCLASS="emphasis">which</EM> show aliases that you've typed at a promptand haven't stored in <EMCLASS="emphasis">.cshrc</EM>.The new <EMCLASS="emphasis">which</EM> also works in<SPANCLASS="link">Bourne-like shells (<ACLASS="linkend"HREF="ch01_08.htm"TITLE="There Are Many Shells ">1.8</A>)</SPAN>that have<SPANCLASS="link">shell functions (<ACLASS="linkend"HREF="ch10_09.htm"TITLE="Shell Functions ">10.9</A>)</SPAN>.</TD></TR></TABLE><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="csh_init">csh_init</A><BR><ACLASS="programreference"HREF="examples/index.htm"TITLE="sh_init">sh_init</A><BR></TH><TDVALIGN="TOP">To make the new <EMCLASS="emphasis">which</EM> read your current aliases, you need a trick.<ACLASS="indexterm"NAME="AUTOID-59423"></A><ACLASS="indexterm"NAME="AUTOID-59426"></A>Here's the trick: make an alias or shell function that runs <EMCLASS="emphasis">which</EM>,passing the definition (if any) of the alias or function you name.</TD></TR></TABLE><PCLASS="para">Let's look at the setup, then explain it.For the C shell, use the following line in your <EMCLASS="emphasis">.cshrc</EM> file:</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-59435"></A><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch10_03.htm"TITLE="10.3 C Shell Aliases with Command-Line Arguments ">!\$</A> <ACLASS="co"HREF="ch10_03.htm"TITLE="10.3 C Shell Aliases with Command-Line Arguments ">!\*</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">alias which alias !\$  /usr/local/bin/which -i !\*</PRE></TD></TR></TABLE></P><PCLASS="para">(There's a similar shell function on the CD-ROM.)For this example, let's say you've also defined an alias for <EMCLASS="emphasis">sort</EM>that looks like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias sort /usr/local/bin/quicksort</PRE></BLOCKQUOTE></P><PCLASS="para">Okay.To run <EMCLASS="emphasis">which</EM>, you type:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>which sort</B></CODE>sort    /usr/local/bin/quicksort</PRE></BLOCKQUOTE></P><PCLASS="para">How did that work?The C shell runs the alias you defined for <EMCLASS="emphasis">which</EM>.In this example, that executes the following command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias sort | /usr/local/bin/which -i sort</PRE></BLOCKQUOTE></P><PCLASS="para">The first part of that command,<CODECLASS="literal">alias&nbsp;sort</CODE>, will pipe the definition of the <EMCLASS="emphasis">sort</EM> alias to thestandard input of <CODECLASS="literal">/usr/local/bin/which&nbsp;-i&nbsp;sort</CODE>.When <EMCLASS="emphasis">/usr/local/bin/which&nbsp;-i&nbsp;sort</EM> sees an alias definition on itsstandard input, it outputs that definition.</P><PCLASS="para">What if you ask <EMCLASS="emphasis">which</EM> to find a command that you haven't aliased?</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>which tr</B></CODE>/bin/tr</PRE></BLOCKQUOTE></P><PCLASS="para">The shell runs this command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias tr | /usr/local/bin/which -i tr</PRE></BLOCKQUOTE></P><PCLASS="para">Because there's no alias for <EMCLASS="emphasis">tr</EM>, the shell command <CODECLASS="literal">alias&nbsp;tr</CODE>sends no output down the pipe.When <CODECLASS="literal">/usr/local/bin/which&nbsp;-i&nbsp;tr</CODE> doesn't read text on standardinput, it looks through your search path for the first command named <EMCLASS="emphasis">tr</EM>.</P><PCLASS="para">Nice trick, isn't it?Maarten Litmaath, the program's author, is a clever guy.</P><PCLASS="para">That's not all the new <EMCLASS="emphasis">which</EM> can do.With the <EMCLASS="emphasis">-a</EM> option, it shows any alias you name and <EMCLASS="emphasis">also</EM>searches your path for <EMCLASS="emphasis">all</EM> commands with that name.This is useful when you want to know all available versions of the command.Let's end this article with an example from the manual page.The first command shows all aliases (in this case, that's just thealias for the new <EMCLASS="emphasis">which</EM>).Second, we run the new <EMCLASS="emphasis">which</EM> to find which <EMCLASS="emphasis">which</EM> we're running<CODECLASS="literal">:-)</CODE>; it shows the alias for <EMCLASS="emphasis">which</EM>.Third, the <EMCLASS="emphasis">-a</EM> option shows all available <EMCLASS="emphasis">which</EM>es:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>alias</B></CODE>which   alias !$ | /usr/local/bin/which -i !*% <CODECLASS="userinput"><B>which which</B></CODE>which   alias !$ | /usr/local/bin/which -i !*% <CODECLASS="userinput"><B>which -a which</B></CODE>which   alias !$ | /usr/local/bin/which -i !*/usr/local/bin/which/usr/ucb/which%</PRE></BLOCKQUOTE></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN>, <SPANCLASS="authorinitials">JP,&nbsp;MAL</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="ch50_07.htm"TITLE="50.7 How UNIX Systems Remember Their Name "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 50.7 How UNIX Systems Remember Their Name "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="ch50_09.htm"TITLE="50.9 Reading a Permuted Index "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 50.9 Reading a Permuted Index "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">50.7 How UNIX Systems Remember Their Name </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">50.9 Reading a Permuted Index </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 + -