home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume03 / which2 < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  1.7 KB

  1. Path: uunet!lll-winken!lll-tis!ames!necntc!ncoast!allbery
  2. From: steven@cwi.nl.UUCP (Steven Pemberton)
  3. Newsgroups: comp.sources.misc
  4. Subject: v03i042: another fast 'which(ucb)' command
  5. Message-ID: <347@piring.cwi.nl>
  6. Date: 3 Jun 88 15:43:47 GMT
  7. Sender: allbery@ncoast.UUCP
  8. Reply-To: steven@cwi.nl.UUCP (Steven Pemberton)
  9. Organization: CWI, Amsterdam
  10. Lines: 48
  11. Approved: allbery@ncoast.UUCP
  12.  
  13. comp.sources.misc: Volume 3, Issue 42
  14. Submitted-By: "Steven Pemberton" <steven@cwi.nl.UUCP>
  15. Archive-Name: which2
  16.  
  17. Here is the version of 'which' that I use. Advantages:
  18.     - twice as fast as the ucb version
  19.     - still a shell script
  20.     - tells you about ALL versions reachable from your path,
  21.       not just the first
  22.     - silent if it finds nothing, so you can use it in `` expansions
  23.         e.g. for f in `which which`; do file $f; done
  24.     - (this is the biggy:) wildcards work!
  25.         which '*uu*' gives all commands reachable with uu in the name.
  26.         which '?'    gives all 1 character commands
  27.         which '*'    gives *all* commands.
  28.       Don't forget the quotes if you use wildcards.
  29.  
  30. It only works if 'test -x' works for you.
  31.  
  32. Steven Pemberton, CWI, Amsterdam; steven@cwi.nl
  33.  
  34. Here's the source:
  35.  
  36. -------------------- Cut here -------------------------
  37. # A faster version of which, that also prints ALL versions in your PATH,
  38. # and accepts wildcards, e.g.: which '*uu*'. Silent if nothing found.
  39. # Only works if test -x works...
  40.  
  41. case $# in
  42. 0) echo Usage: $0 cmd ...; exit 1;;
  43. esac
  44.  
  45. dirs=`echo $PATH|sed 's/^:/. /
  46.      s/:$/ ./
  47.      s/::/ . /g
  48.      s/:/ /g'`
  49.  
  50. for cmd
  51. do
  52.    for d in $dirs
  53.    do
  54.       for file in $d/$cmd
  55.       do
  56.          if test -x $file -a ! -d $file
  57.          then echo $file
  58.          fi
  59.       done
  60.    done
  61. done
  62.