You cannot use command_not_found_handle()
because the commands do exist; they just return an error ($? != 0
) because of unrecognized parameters.
In these kinds of situations you should explicitly control the commands issued with a platform-specific prologue at the very beginning of the script, making the minimum number of assumptions about parameters, e.g.:
# defaults - satisfies most platforms
MD5SUM=md5sum
UNIQ=uniq
# per-platform exceptions
if [ "$OS" == "MacOS" ] ; then
# use GNU variants by prefixing with g
MD5SUM="g $MD5SUM" # or use the native program, MD5SUM=md5
UNIQ="g $UNIQ" # or use the native program, UNIQ=uniq
elif [ "$OS" == "SomeOtherOS" ] ; then
MD5SUM=md5hash # e.g. this platform computes MD5 sums with md5hash
fi
...
# optionally cross-check for GNU variants on all platforms
# does uniq support -w32? (no error on dry run?) if so, use it!
$UNIQ -w32 /dev/null 2>/dev/null && UNIQ="$UNIQ -w32"
# optionally perform one last validation before proceeding
die() { echo "FATAL: $*" >&2 ; exit $1 ; }
$UNIQ /dev/null >/dev/null || die $? "uniq is missing or failing: $UNIQ"
$MD5SUM /dev/null >/dev/null || die $? "md5sum is missing or failing: $MD5SUM"
...
$MD5SUM *.java | $UNIQ -d
UPDATE
Note that it is also good practice, especially if your script will run as root
, to not rely on $PATH
-- you would want to specify the fully qualified locations for programs such as uniq
or md5
or md5sum
above, e.g. UNIQ=/bin/uniq
, MD5SUM=/sbin/md5sum
, etc.)
Cheers,
V.
UPDATE2
To automatically prefix a bunch of commands with "g":
eval `for command in uniq md5sum grep sed ; do
echo \`echo \$command | tr '[a-z]' '[A-Z]'\`=\"g \$command\"
done`