📄 ch50_03.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 50] 50.3 apropos on Systems Without apropos </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:56:50Z"><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_02.htm"TITLE="50.2 The apropos Command "><LINKREL="next"HREF="ch50_04.htm"TITLE="50.4 whatis: One-Line Command Summaries "></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_02.htm"TITLE="50.2 The apropos Command "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 50.2 The apropos Command "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_04.htm"TITLE="50.4 whatis: One-Line Command Summaries "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 50.4 whatis: One-Line Command Summaries "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-2101">50.3 apropos on Systems Without apropos </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-2101-IX-APROPOS-COMMAND-ALTERNATIVES-TO"></A><ACLASS="indexterm"NAME="UPT-ART-2101-IX-APROPOS-COMMAND-SUPPORT-FOR"></A><ACLASS="indexterm"NAME="UPT-ART-2101-IX-MANUAL-PAGES-SEARCHING"></A><ACLASS="indexterm"NAME="UPT-ART-2101-IX-SEARCHING-MANUAL-PAGES"></A><ACLASS="indexterm"NAME="AUTOID-59058"></A><ACLASS="indexterm"NAME="AUTOID-59061"></A>I was just asked to write an article about how to simulate<EMCLASS="emphasis">apropos</EM> on systems that don't have it.I have to confess thatI've never faced this problem.But I was able to come up with asolution.Your mileage may vary - particularly since different UNIXimplementations have different ways of storing their manual pages.</P><PCLASS="para">The solution has two parts.First, you need a script that builds anindex; that's better than <EMCLASS="emphasis">grep</EM>ping through every manual page in theworld.[<SPANCLASS="link">Not always (<ACLASS="linkend"HREF="ch50_06.htm"TITLE="Searching Online Manual Pages ">50.6</A>)</SPAN>.<CODECLASS="literal">;-)</CODE> -<EMCLASS="emphasis">JP</EM> ]Then you need an <EMCLASS="emphasis">apropos</EM> alias that automatically searchesyour index file.Here's the script to build the index file:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch09_12.htm"TITLE="9.12 The Bourne Shell for Loop ">for</A> <ACLASS="co"HREF="ch34_24.htm"TITLE="34.24 Quick Reference: sed ">sed</A> <ACLASS="co"HREF="ch09_16.htm"TITLE="9.16 Command Substitution ">`...`</A> <ACLASS="co"HREF="ch43_18.htm"TITLE="43.18 How nroff Makes Bold and Underline; How to Remove It">col</A> <ACLASS="co"HREF="ch27_05.htm"TITLE="27.5 Extended Searching for Text with egrep ">egrep</A> <ACLASS="co"HREF="ch35_20.htm"TITLE="35.20 Quick Reference: uniq ">uniq</A> <ACLASS="co"HREF="ch45_23.htm"TITLE="45.23 The Ins and Outs of Redirected I/O Loops ">done ></A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"><ACLASS="indexterm"NAME="AUTOID-59073"></A>#!/bin/sh# manindex: Generate a list of topic lines that you can grep through.# Then create 'apropos' and other aliases to search the list.# Run this periodically-once a month should sufficemandir=/usr/share/man # where the manual pages are storedmanlist="cat1 cat2 cat3" # list particular directories you care aboutindexfile="/home/mike/manindex.txt"rm -f $indexfile<ACLASS="indexterm"NAME="AUTOID-59076"></A><ACLASS="indexterm"NAME="AUTOID-59079"></A><ACLASS="indexterm"NAME="AUTOID-59082"></A><ACLASS="indexterm"NAME="AUTOID-59085"></A><ACLASS="indexterm"NAME="AUTOID-59087"></A><ACLASS="indexterm"NAME="AUTOID-59090"></A><ACLASS="indexterm"NAME="AUTOID-59092"></A>for directory in $manlistdo cd $mandir/$directory # the sed command turns filenames into "manual page" names # e.g., converts sed.1.z to sed. # BUG: won't handle names like a.out.4.Z correctly for manpage in `ls | sed -e 's/\..*$//g'` do # use man to unpack the manual page; it might be compressed # use col to strip garbage characters # egrep looks for spaces, manual page name, and dash man $manpage | col -b -x | egrep "^ +$manpage.* - " | uniq donedone > $indexfile</PRE></TD></TR></TABLE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-59102"></A><ACLASS="indexterm"NAME="AUTOID-59104"></A>This script goes through every directory in which manual pages arestored.It strips all suffixes from the filenames, and then uses<EMCLASS="emphasis">man</EM> to print the actual manual page.This is better than tryingto look at the raw manual pages themselves because some vendors don'tprovide the raw manual pages.If you let <EMCLASS="emphasis">man</EM> give you the page youwant, you'll always find it.[2]The <EMCLASS="emphasis">col</EM>command strips out boldfacing, underlining, and other monstrositiesthat will confuse <EMCLASS="emphasis">grep</EM>.Finally, <EMCLASS="emphasis">egrep</EM> looks for linesto put in the index.It's not as fussy as the BSD <EMCLASS="emphasis">catman</EM>program (which <EMCLASS="emphasis">mkindex</EM> is emulating), so it will find a fairnumber of lines that don't belong; but we think this is only a mildflaw.</P><BLOCKQUOTECLASS="footnote"><PCLASS="para">[2] If you have the source files for the manual page online, rather thanpre-formatted files, you might want to rewrite the script to searchthem directly.It will be a lot faster.</P></BLOCKQUOTE><PCLASS="para">Before you can use this scriptyou'll have to substitute definitions for three variables:</P><DLCLASS="variablelist"><DTCLASS="term"><EMCLASS="emphasis">mandir</EM></DT><DDCLASS="listitem"><PCLASS="para">The top-level directory in which manual pages are stored.Often<EMCLASS="emphasis">/usr/man</EM>, but it may be different on your system.</P></DD><DTCLASS="term"><EMCLASS="emphasis">manlist</EM></DT><DDCLASS="listitem"><PCLASS="para">Subdirectories containing the manual pages you care about.You'llprobably want the directory in which user-level commands are stored,as a minimum.This level of directory naming may be radicallydifferent on different systems.I think this script is flexibleenough to handle all the variations I can remember; if it can't,you'll have to hack it up a bit.</P></DD><DTCLASS="term"><EMCLASS="emphasis">indexfile</EM></DT><DDCLASS="listitem"><PCLASS="para">The file in which you want to keep your index (the output of this script).</P></DD></DL><PCLASS="para">Expect <EMCLASS="emphasis">manindex</EM> to run for a long time; several minutes ormore, depending on how thorough you want to be.Fortunately, you don't need to do this veryoften: once to get started, and then once a month (or so) to pick upany "stray" manual pages that someone else may have added.If you wantto speed the task up, remember that you can skip any sections of themanual that you're not interested in by defining <EMCLASS="emphasis">manlist</EM>appropriately.For example, if you're not interested in section 2 ofthe manual, just leave <EMCLASS="emphasis">cat2</EM> out of the list.</P><PCLASS="para">Once you've created the index, the rest of the problem is easy.Justmake yourself an <EMCLASS="emphasis">apropos</EM><SPANCLASS="link">alias (<ACLASS="linkend"HREF="ch10_02.htm"TITLE="Aliases for Common Commands ">10.2</A>)</SPAN>and add it to your <EMCLASS="emphasis">.cshrc</EM>file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">alias apropos "grep -i \!$ /home/mike/manindex.txt"</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-59145"></A>Here's what its output looks like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>apropos search</B></CODE>acctcom - search and print process accounting file(s)egrep - search a file for a pattern using full regular expressionsfgrep - search a file for a character stringfmlgrep - search a file for a patterngrep - search a file for a patternpathconv - search FMLI criteria for filename</PRE></BLOCKQUOTE></P><PCLASS="para">As I pointed out, this isn't perfect.But I think it's a reasonablesubstitute.<ACLASS="indexterm"NAME="AUTOID-59152"></A><ACLASS="indexterm"NAME="AUTOID-59153"></A><ACLASS="indexterm"NAME="AUTOID-59154"></A><ACLASS="indexterm"NAME="AUTOID-59155"></A></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="ch50_02.htm"TITLE="50.2 The apropos Command "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 50.2 The apropos Command "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_04.htm"TITLE="50.4 whatis: One-Line Command Summaries "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 50.4 whatis: One-Line Command Summaries "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">50.2 The apropos Command </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.4 whatis: One-Line Command Summaries </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 & 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 + -