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

📄 ch14_09.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 14] 14.9 cd by Directory Initials </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:06Z"><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_08.htm"TITLE="14.8 Quick cds with Aliases "><LINKREL="next"HREF="ch14_10.htm"TITLE="14.10 Variables Help You Find Directories and Files "></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_08.htm"TITLE="14.8 Quick cds with Aliases "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.8 Quick cds with Aliases "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_10.htm"TITLE="14.10 Variables Help You Find Directories and Files "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.10 Variables Help You Find Directories and Files "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7901">14.9 cd by Directory Initials </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-15514"></A><ACLASS="indexterm"NAME="AUTOID-15517"></A>Here's a handy shell function called <EMCLASS="emphasis">c</EM> for people who <EMCLASS="emphasis">cd</EM> all over the filesystem.(I first saw Marc Brumlik's posting of it on Usenet years ago, as aC shell alias.He and I have both made some changes to it since then.)This function is great for shells that don't have<SPANCLASS="link">filename completion (<ACLASS="linkend"HREF="ch09_08.htm"TITLE="Filename Completion: Faster Filename Typing ">9.8</A>)</SPAN>.This function works a bit likefilename completion,but it's faster because the &quot;initials&quot; only match directories&nbsp;- and you don'thave to press TAB or ESC after each part of the pathname.Instead, you just type the initials (first letter, or more) of each directoryin the pathname.Start at the root directory.Put a dot (<CODECLASS="literal">.</CODE>) after each part.</P><PCLASS="para">Here are three examples.The first one shows that there's no subdirectory of root whose namestarts with <EMCLASS="emphasis">q</EM>.The second one matches the directory <EMCLASS="emphasis">/usr/include/hsfs</EM> and <EMCLASS="emphasis">cd</EM>sthere:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>c q.</B></CODE>c: no match for /q*/.$ <CODECLASS="userinput"><B>c u.i.h.</B></CODE>/usr/include/hsfs/.$</PRE></BLOCKQUOTE></P><PCLASS="para">In the next example, trying to change to <EMCLASS="emphasis">/usr/include/pascal</EM>, the abbreviations aren't unique the first time.The function shows me all the matches; the second time, I add another letter(&quot;a&quot;) to make the name unique:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>c u.i.p.</B></CODE>c: too many matches for u.i.p.:/usr/include/pascal/. /usr/include/pixrect/. /usr/include/protocols/.$ <CODECLASS="userinput"><B>c u.i.pa.</B></CODE>/usr/include/pascal/.$</PRE></BLOCKQUOTE></P><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="sh_init">sh_init</A><BR><ACLASS="programreference"HREF="examples/index.htm"TITLE="csh_init">csh_init</A><BR></TH><TDVALIGN="TOP">&#13;The Bourne shell function is straightforward; it's shown below.The C shell alias needs some trickery, and there are two versions of iton the Power Tools disk: one if you already have an alias for <EMCLASS="emphasis">cd</EM>and another if you don't.(The C shell<SPANCLASS="link"><EMCLASS="emphasis">if</EM> (<ACLASS="linkend"HREF="ch47_03.htm"TITLE="Conditional Statements with if ">47.3</A>)</SPAN>used in the <EMCLASS="emphasis">c</EM> alias won't work with a <EMCLASS="emphasis">cd</EM> alias.Although the <EMCLASS="emphasis">csh</EM> manual page admits it won't work, I'd call that another<SPANCLASS="link">C shell bug (<ACLASS="linkend"HREF="ch47_02.htm"TITLE="C Shell Programming Considered Harmful ">47.2</A>)</SPAN>.)</TD></TR></TABLE><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch44_19.htm"TITLE="44.19 The Bourne Shell set Command ">set</A> <ACLASS="co"HREF="ch44_15.htm"TITLE="44.15 Handling Command-Line Arguments in Shell Scripts ">$#</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">c(){   dir=&quot;$1&quot;   # Delete dots.  Surround every letter with &quot;/&quot; and &quot;*&quot;.   # Add a final &quot;/.&quot; to be sure this only matches a directory:   dirpat=&quot;`echo $dir | sed 's/\([^.]*\)\./\/\1*/g'`/.&quot;   # In case $dirpat is empty, set dummy &quot;x&quot; then shift it away:   set x $dirpat; shift   # Do the cd if we got one match, else print error:   if [ &quot;$1&quot; = &quot;$dirpat&quot; ]; then      # pattern didn't match (shell didn't expand it)      echo &quot;c: no match for $dirpat&quot; 1&gt;&amp;2   elif [ $# = 1 ]; then      echo &quot;$1&quot;      cd &quot;$1&quot;   else      echo &quot;c: too many matches for $dir:&quot; 1&gt;&amp;2      ls -d &quot;$@&quot;   fi   unset dir dirpat}</PRE></TD></TR></TABLE></P><PCLASS="para">The function starts by building a wildcard pattern to match thedirectory initials.For example, if you type <CODECLASS="literal">c&nbsp;u.i.h.</CODE>, <EMCLASS="emphasis">sed</EM> makes the pattern<CODECLASS="literal">/u*/i*/h*/.</CODE> in <CODECLASS="literal">$dirpat</CODE>.Next, the shell expands the wildcards onto its command-line parameters;the trailing dot makes sure the pattern only matches a directory (as inarticle<ACLASS="xref"HREF="ch21_12.htm"TITLE="Copying Files to a Directory ">21.12</A>).If the Bourne shell can't match a wildcard pattern, it leaves thepattern unchanged; the first <CODECLASS="literal">if</CODE> test spots that.If there was just one match, there'll be one command-line parameterleft, and the shell <EMCLASS="emphasis">cd</EM>s there.Otherwise, there were too many matches; the function shows them so youcan make your pattern longer and more specific.</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_08.htm"TITLE="14.8 Quick cds with Aliases "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 14.8 Quick cds with Aliases "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_10.htm"TITLE="14.10 Variables Help You Find Directories and Files "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 14.10 Variables Help You Find Directories and Files "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">14.8 Quick cds with Aliases </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.10 Variables Help You Find Directories and Files </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 + -