Which script

Doug Gwyn gwyn at smoke.BRL.MIL
Sun Sep 23 20:10:04 AEST 1990


In article <MEISSNER.90Sep22142134 at osf.osf.org>, meissner at 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



More information about the Comp.unix.questions mailing list