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 # -g <re> Search string in <args> output. Default <prog>.
8 # -q Don't print path to prog. Just exit 0 if found.
9 # -x Don't try to execute unreadable files. Assume success.
10 # -X Don't try to execute unreadable files. Assume failure.
17 while getopts ":Aa:g:qxX" opt; do
30 if [ -z "$prog" ]; then
31 echo >&2 "Usage: find_working [options] <prog>"
32 echo >&2 "Options: -A Don't test arguments to executable files."
33 echo >&2 " -a <args> Use arguments to test executable files. Default --help."
34 echo >&2 " -g <re> Search string in <args> output. Default <prog>."
35 echo >&2 " -q Don't print path to prog. Just exit 0 if found."
36 echo >&2 " -x Don't try to execute unreadable files. Assume success."
37 echo >&2 " -X Don't try to execute unreadable files. Assume failure."
41 [ -z "$re" ] && re="\\b$prog\\b"
43 # Default args contain -? which might be interpreted as a glob.
47 for path in ${PATH//:/ }; do
48 [ -x "$path/$prog" ] || continue
50 if [ -r "$path/$prog" ]; then
51 if [ -n "$args" ]; then
52 "$path/$prog" $args 2>&1 | grep "$re" >/dev/null || continue
54 ldd "$path/$prog" 2>/dev/null | grep "not found" >/dev/null && continue
58 elif [ -z "$unreadable" ]; then
59 "$path/$prog" $args 2>&1 | grep "$re" >/dev/null || continue
62 elif [ $unreadable = 0 ]; then
65 elif [ $unreadable = 1 ]; then
72 if [ -n "$ret" ]; then
73 [ $quiet = 0 ] && echo "$ret"