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

📄 ch16_10.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 16] 16.10 findcmd: Find a Command in 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:37:43Z"><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="ch16_01.htm"TITLE="16. Where Did I Put That?"><LINKREL="prev"HREF="ch16_09.htm"TITLE="16.9 An Alias to List Recently Changed Files "><LINKREL="next"HREF="ch16_11.htm"TITLE="16.11 Showing Hidden Files with ls -A and -a "></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="ch16_09.htm"TITLE="16.9 An Alias to List Recently Changed Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 16.9 An Alias to List Recently Changed Files "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 16<BR>Where Did I Put That?</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch16_11.htm"TITLE="16.11 Showing Hidden Files with ls -A and -a "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 16.11 Showing Hidden Files with ls -A and -a "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-4300">16.10 findcmd: Find a Command in Your Search Path </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-17105"></A><ACLASS="indexterm"NAME="AUTOID-17107"></A><ACLASS="indexterm"NAME="AUTOID-17109"></A><ACLASS="indexterm"NAME="AUTOID-17112"></A>UNIX has utilities like <SPANCLASS="link"><EMCLASS="emphasis">whereis</EM> (<ACLASS="linkend"HREF="ch50_05.htm"TITLE="whereis: Finding Where a Command Is Located ">50.5</A>)</SPAN>and <SPANCLASS="link"><EMCLASS="emphasis">which</EM> (<ACLASS="linkend"HREF="ch50_08.htm"TITLE="Which Version Am I Using? ">50.8</A>)</SPAN>to look fora command on the system.But <EMCLASS="emphasis">whereis</EM> doesn't look in your shell's search path, so itmay not find shell scripts in local system directories or your<SPANCLASS="link"><EMCLASS="emphasis">bin</EM> directory (<ACLASS="linkend"HREF="ch04_02.htm"TITLE="A bin Directory for Your Programs and Scripts ">4.2</A>)</SPAN>.And to use <EMCLASS="emphasis">which</EM>, you have to know the exact name of the command,because <EMCLASS="emphasis">which</EM> only shows the first command with that name in your path.</P><PCLASS="para">If you're like me, you can't always remember the name of the commandyou're looking for.&quot;Wasn't it called <EMCLASS="emphasis">reference</EM> or <EMCLASS="emphasis">refer</EM> or something like that?&quot;<ACLASS="indexterm"NAME="AUTOID-17127"></A>The <EMCLASS="emphasis">findcmd</EM> script saves me a lot of guessing.It shows all command names, in all directoriesin my search path, that contain some string.So, I'll look for command names that have &quot;ref&quot; in them:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>findcmd ref</B></CODE>/home/jerry/.bin/zrefile/usr/bin/X11/xrefresh/usr/local/bin/grefer/bin/cxref/bin/refer/usr/bin/cxref/usr/bin/refer./preferences</PRE></BLOCKQUOTE></P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="findcmd">findcmd</A><BR></TH><TDVALIGN="TOP">After a couple of tries, I usually find the command I want.The <EMCLASS="emphasis">findcmd</EM> script is on the CD-ROM.</TD></TR></TABLE><PCLASS="para">First, the script edits a copy of your<SPANCLASS="link"><EMCLASS="emphasis">PATH</EM> (<ACLASS="linkend"HREF="ch06_04.htm"TITLE="The PATH Environment Variable ">6.4</A>)</SPAN>to change any current directory entry to a dot (<CODECLASS="literal">:.:</CODE>).Next, a colon (<CODECLASS="literal">:</CODE>) in the<SPANCLASS="link"><EMCLASS="emphasis">IFS</EM> (<ACLASS="linkend"HREF="ch35_21.htm"TITLE="Using IFS to Split Strings ">35.21</A>)</SPAN>variable lets the shell split the <EMCLASS="emphasis">PATH</EM> at the colons;a<SPANCLASS="link"><EMCLASS="emphasis">for</EM> loop (<ACLASS="linkend"HREF="ch44_16.htm"TITLE="Handling Command-Line Arguments with a for Loop ">44.16</A>)</SPAN>steps through each directory in the <EMCLASS="emphasis">PATH</EM>and runs <EMCLASS="emphasis">ls -l</EM> to find matching files.Finally, a<SPANCLASS="link"><EMCLASS="emphasis">sed</EM> (<ACLASS="linkend"HREF="ch34_24.htm"TITLE="Quick Reference: sed ">34.24</A>)</SPAN>script reads through the output of all the <EMCLASS="emphasis">ls</EM>commands in the loop, editing and printing matching lines (executable files withthe program name we want).</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="ch16_09.htm"TITLE="16.9 An Alias to List Recently Changed Files "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 16.9 An Alias to List Recently Changed Files "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="ch16_11.htm"TITLE="16.11 Showing Hidden Files with ls -A and -a "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 16.11 Showing Hidden Files with ls -A and -a "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">16.9 An Alias to List Recently Changed Files </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">16.11 Showing Hidden Files with ls -A and -a </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 + -