Subject: Re: replacement for which(1)
To: Bill Studenmund <wrstuden@netbsd.org>
From: Simon J. Gerraty <sjg@crufty.net>
List: tech-userlevel
Date: 04/01/2004 14:26:56
>That and built-ins are the two reasons I think we shouldn't try to use an=
>=20
>external command.
Yes and no. Many scripts want to simply - and exactly, locate a binary
via $PATH or know that no such binary exists.
The existing which(1) is inappropriate due since its a csh script
and follows the wrong semantics for the above.
Shell builtins like type, whence etc, are also not very useful since every
shell has a different one, and not all of them do just the above.
Another external command might be good - but is useless unless it is widely
available with the correct semantics.
The solution I've used for years is:
#!/bin/sh
Which() {
case "$1" in
-*) t=$1; shift;;
*) t=-x;;
esac
case "$1" in
/*) test $t $1 && echo $1;;
*)
for d in `IFS=:; echo ${2:-$PATH}`
do
test $t $d/$1 && { echo $d/$1; break; }
done
;;
esac
}
case /$0. in
*/[wW]hich.*) Which "$@";;
esac
Which you can place in a file (which.sh) and use as an external command
or . to get the function - depending on how many times you need to use it.
--sjg