From: Iain Patterson Date: Thu, 13 Jun 2013 09:29:16 +0000 (+0100) Subject: Allow specifying a match in program output. X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=24473107a8819cc3b2d34d0a2e681a94b674d5bb;p=profile.git Allow specifying a match in program output. Use the -g flag to specify a string to grep for instead of just the program name. --- diff --git a/opt/bin/find_working b/opt/bin/find_working index 77bf17e..454e365 100755 --- a/opt/bin/find_working +++ b/opt/bin/find_working @@ -4,6 +4,7 @@ # Usage: find_working [options] # Options: -A Don't test arguments to executable files. # -a Use arguments to test executable files. Default --help. +# -g Search string in output. Default . # -q Don't print path to prog. Just exit 0 if found. # -x Don't try to execute unreadable files. Assume success. # -X Don't try to execute unreadable files. Assume failure. @@ -12,10 +13,12 @@ args="-h -? --help" quiet=0 unreadable= -while getopts ":Aa:qxX" opt; do +re= +while getopts ":Aa:g:qxX" opt; do case $opt in A) args="";; a) args="$OPTARG";; + g) re="$OPTARG";; q) quiet=1;; x) unreadable=0;; X) unreadable=1;; @@ -28,12 +31,15 @@ if [ -z "$prog" ]; then echo >&2 "Usage: find_working [options] " echo >&2 "Options: -A Don't test arguments to executable files." echo >&2 " -a Use arguments to test executable files. Default --help." + echo >&2 " -g Search string in output. Default ." echo >&2 " -q Don't print path to prog. Just exit 0 if found." echo >&2 " -x Don't try to execute unreadable files. Assume success." echo >&2 " -X Don't try to execute unreadable files. Assume failure." exit 1 fi +[ -z "$re" ] && re="\\b$prog\\b" + # Default args contain -? which might be interpreted as a glob. set -o noglob @@ -43,14 +49,14 @@ for path in ${PATH//:/ }; do if [ -r "$path/$prog" ]; then if [ -n "$args" ]; then - "$path/$prog" $args 2>&1 | grep "\\b$prog\\b" >/dev/null || continue + "$path/$prog" $args 2>&1 | grep "$re" >/dev/null || continue else ldd "$path/$prog" 2>/dev/null | grep "not found" >/dev/null && continue fi ret="$path/$prog" break elif [ -z "$unreadable" ]; then - "$path/$prog" $args 2>&1 | grep "\\b$prog\\b" >/dev/null || continue + "$path/$prog" $args 2>&1 | grep "$re" >/dev/null || continue ret="$path/$prog" break elif [ $unreadable = 0 ]; then