3 # find_working: Find a version of some tool in the PATH which actually runs.
4 # Usage: find_working [options] <prog>
5 # Options: -A Don't test arguments to executable files.
6 # -a <args> Use arguments to test executable files. Default --help.
7 # -q Don't print path to prog. Just exit 0 if found.
8 # -x Don't try to execute unreadable files. Assume success.
9 # -X Don't try to execute unreadable files. Assume failure.
15 while getopts ":Aa:qxX" opt; do
27 if [ -z "$prog" ]; then
28 echo >&2 "Usage: find_working [options] <prog>"
29 echo >&2 "Options: -A Don't test arguments to executable files."
30 echo >&2 " -a <args> Use arguments to test executable files. Default --help."
31 echo >&2 " -q Don't print path to prog. Just exit 0 if found."
32 echo >&2 " -x Don't try to execute unreadable files. Assume success."
33 echo >&2 " -X Don't try to execute unreadable files. Assume failure."
37 # Default args contain -? which might be interpreted as a glob.
41 for path in ${PATH//:/ }; do
42 [ -x "$path/$prog" ] || continue
44 if [ -r "$path/$prog" ]; then
45 if [ -n "$args" ]; then
46 "$path/$prog" $args 2>&1 | grep "\\b$prog\\b" >/dev/null || continue
48 ldd "$path/$prog" 2>/dev/null | grep "not found" >/dev/null && continue
52 elif [ -z "$unreadable" ]; then
53 "$path/$prog" $args 2>&1 | grep "\\b$prog\\b" >/dev/null || continue
56 elif [ $unreadable = 0 ]; then
59 elif [ $unreadable = 1 ]; then
66 if [ -n "$ret" ]; then
67 [ $quiet = 0 ] && echo "$ret"