A pattern you'll typically see with Unix shell scripts is the use of a variable for the executables named by their generic function, for example $ECHO
. You'll then have a top level function or two that will determine what $ECHO
should be set to.
Once $ECHO
has been set, then throughout your code you'll see this:
$ECHO "hi"
This saves you from having to litter your code with lots of if..then..else blocks which have nothing to do with your scripts functionality and only the platform interpreter that it's using.
So you could do the same for any key programs that you're interested in swapping out based on which interpreter/platform you're running on.
if (ksh)
then
ECHO=echo1
FILEFIND=whence
else if (sh)
ECHO=echo2
FILEFIND=whereis
else
ECHO=echo3
FILEFIND=which
fi
Should you do this?
I would say if you're trying to make your script portable and you're concerned with the availability of certain interpreters or executables it's best to code to the lowest common denominator, i.e. /bin/sh
.
Just look at how Oracle and others do this. They generally provide their installers so that they're scripted in /bin/sh
. This is the lowest common denominator when you get into supporting across a variety of Unixes such as Solaris, AIX, and Linux.
Take a look at this guide put together by Novell titled: Making Scripts Portable. It covers the lineage of the various shells and what the common set of commands are across several of them with tips on how to deal with their subtleties.