3 # find_working: Find a version of some tool in the PATH which actually runs.
4 # Usage: find_working [options] <prog>
5 # Options: -a <args> Use arguments to test executable files. Default --help.
6 # -A Don't test arguments to executable files.
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 ":a:AqxX" opt; do
27 if [ -z "$prog" ]; then
28 echo >&2 "Usage: find_working [options] <prog>"
29 echo >&2 "Options: -a <args> Use arguments to test executable files. Default --help."
30 echo >&2 " -A Don't test arguments to executable files."
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."
38 for path in ${PATH//:/ }; do
39 [ -x "$path/$prog" ] || continue
41 if [ -r "$path/$prog" ]; then
42 if [ -n "$args" ]; then
43 "$path/$prog" $args 2>&1 | grep " $prog " >/dev/null || continue
45 ldd "$path/$prog" 2>/dev/null | grep "not found" >/dev/null && continue
49 elif [ -z "$unreadable" ]; then
50 "$path/$prog" $args 2>&1 | grep " $prog " >/dev/null || continue
53 elif [ $unreadable = 0 ]; then
56 elif [ $unreadable = 1 ]; then
63 if [ -n "$ret" ]; then
64 [ $quiet = 0 ] && echo "$ret"