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 # -q Don't print path to prog. Just exit 0 if found.
7 # -x Don't try to execute unreadable files. Assume success.
8 # -X Don't try to execute unreadable files. Assume failure.
14 while getopts ":a:qxX" opt; do
25 if [ -z "$prog" ]; then
26 echo >&2 "Usage: find_working [options] <prog>"
27 echo >&2 "Options: -a <args> Use arguments to test executable files. Default --help."
28 echo >&2 " -q Don't print path to prog. Just exit 0 if found."
29 echo >&2 " -x Don't try to execute unreadable files. Assume success."
30 echo >&2 " -X Don't try to execute unreadable files. Assume failure."
35 for path in ${PATH//:/ }; do
36 [ -x "$path/$prog" ] || continue
38 if [ -r "$path/$prog" ]; then
39 ldd "$path/$prog" 2>/dev/null | grep "not found" >/dev/null && continue
42 elif [ -z "$unreadable" ]; then
43 "$path/$prog" $args 2>&1 | grep " $prog " >/dev/null || continue
46 elif [ $unreadable = 0 ]; then
49 elif [ $unreadable = 1 ]; then
56 if [ -n "$ret" ]; then
57 [ $quiet = 0 ] && echo "$ret"