Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uflorida!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.unix.questions Subject: Re: Which script Message-ID: <13927@smoke.BRL.MIL> Date: 23 Sep 90 10:10:04 GMT References: <574@DIALix.UUCP> <13992@hydra.gatech.EDU> Organization: U.S. Army Ballistic Research Laboratory (BRL), APG, MD. Lines: 55 In article , meissner@osf.org (Michael Meissner) writes: > I like bash's type -all feature, where you can find all occurances of > a command in the PATH. Or, assuming PATH is exported, you can use a shell script like this: #!/usr/5bin/sh # which, every -- which cmd in PATH is executed # adapted from Kernighan & Pike # last edit: 85/03/07 D A Gwyn # SCCS ID: @(#)which.sh 1.4 opath=$PATH PATH=/usr/5bin:/bin:/usr/bin name=`basename $0` # "which" or "every" if [ $# -eq 0 ] then echo Usage: $name 'command(s)' 1>&2 exit 2 fi prefixes=`echo $opath | sed 's/^:/.:/ s/::/:.:/g s/:$/:./ s/:/ /g'` ex=1 # assume nothing found for cmd in $* do nf=1 # assume cmd not found case $cmd in /*) if [ -f $cmd -a -x $cmd ] then echo $cmd nf=0 # found it fi ;; *) for pfx in $prefixes do if [ -f $pfx/$cmd -a -x $pfx/$cmd ] then echo $pfx/$cmd nf=0 # found it if [ $name = which ] then break fi fi done ;; esac if [ $nf -ne 0 ] then echo $name: $cmd: 'not found' 1>&2 else ex=0 # found one fi done exit $ex