[alt.sources.d] which

peterc@softway.sw.oz.au (Peter Chubb) (01/31/91)

My favourite solution, for the Bourne shell, at least, is:

#!/bin/sh
# which -- print full pathname of executable args.

for i
do
    IFS=":"
    for j in $PATH
    do
	test -x $j/$i && echo $j/$i
	exit 0
    done
done
exit 1

For shells with echo and test built in (most of them, now!), this involves 
no external executables.  By omitting the exit statements, all executables
with the given name(s) in the path are printed out.

Of course, it's trivial to convert this into a shell function, but if you 
do, make sure IFS is put back the way it's supposed to be!

Share and enjoy!


			Regards,

				- Peter Chubb

Softway Pty Ltd, P.O. Box 305, Strawberry Hills, NSW 2012, AUSTRALIA
Phone: +61 2 698 2322;       Fax: +61 2 699 9174;     Telex: AA27987
Internet: peterc@softway.oz.au	   UUCP: ...!uunet!softway.oz!peterc

maart@cs.vu.nl (Maarten Litmaath) (02/01/91)

In article <4545@softway.sw.oz.au>,
	peterc@softway.sw.oz.au (Peter Chubb) writes:
)My favourite solution, for the Bourne shell, at least, is:
)
)#!/bin/sh
)# which -- print full pathname of executable args.
)
)for i
)do
)    IFS=":"
)    for j in $PATH
)    do
)	test -x $j/$i && echo $j/$i
)	exit 0
)    done
)done
)exit 1

This script doesn't work at all!  You meant this:

	test -x $j/$i && {
		echo $j/$i
		exit 0
	}

Furthermore this script has the same problems as other scripts that
invoke `test', as discussed before.
--
Temporary files like /tmp/sh$$ are an abomination.