Linux: find first result from specified search path
You could just use a script that simply tests (-e
) for existence of a file and stops when the first one has been found:
#!/bin/bash
[[ $# -gt 0 ]] || { echo "Usage: $0 <filename> [pathspec]" >&2 ; exit 1 ; }
if [[ $# -gt 1 ]] ; then
P="$2"
else
P="$PATH"
fi
IFS=:
for DIR in $P ; do
if [[ -e "$DIR/$1" ]] ; then
echo "$DIR/$1"
exit 0
fi
done
Example:
$ ./search.sh
Usage: ./search.sh <filename> [pathspec]
$ ./search.sh ls
/Users/danielbeck/bin/ls
$ ./search.sh pwd
/bin/pwd
$ ./search.sh ls /bin
/bin/ls
$ ./search.sh ls /usr/bin:/bin
/bin/ls