home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / unix_c / utils / which.sh < prev    next >
Encoding:
Text File  |  1989-03-21  |  1.4 KB  |  54 lines

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