Cover V02, I03
Article
Listing 1
Listing 2
Sidebar 1

may93.tar


Listing 1: The which program

:
#       which
#
#       Examine the path for a command & tell which dir has it.  Stop with
#       the 1st dir that has it.
#
#       Copyright 1990, Lawrence S Reznick

if [ "X$1" = "X" ]
then
echo "usage:\t$0 programnames ..."
exit 1
fi

dirs=`echo $PATH |
sed -e 's/^:/\.:/' -e 's/:$/:\./' -e 's/::/:\.:/' -e 's/:/ /g'`

notfound=

while [ "X$1" != "X" ]
do
found=0

for dir in $dirs
do
if [ -x $dir/$1 ]
then
echo $dir/$1
found=1
break
fi
done

if [ $found -eq 0 ]
then
notfound="$notfound $1"
fi

shift
done

if [ "X$notfound" != "X" ]
then
echo No $notfound in $dirs
fi

exit 0