I'm after THE proper way to see if a function is defined or not. A POSIX compliant way.
__function_defined() {
FUNC_NAME=$1
d=$(declare -f $FUNCNAME)
if [ "${DISTRO_NAME_L}" = "centos" ]; then
if typeset -f $FUNC_NAME &>/dev/null ; then
echo " * INFO: Found function $FUNC_NAME"
return 0
fi
# Try POSIXLY_CORRECT or not
elif test -n "${POSIXLY_CORRECT+yes}"; then
if typeset -f ${FUNC_NAME} >/dev/null 2>&1 ; then
echo " * INFO: Found function $FUNC_NAME"
return 0
fi
else
# Arch linux seems to fall here
if $( type ${FUNC_NAME} >/dev/null 2>&1 ) ; then
echo " * INFO: Found function $FUNC_NAME"
return 0
fi
echo " * INFO: $FUNC_NAME not found...."
return 1
}
All of the above are considered bash'isms according to debian's checkbashisms script.
Trying to grep the script is also not ok. for example:
if [ "$(grep $FUNC_NAME $(dirname $0)/$(basename $0))x" != "x" ]; then
# This is really ugly and counter producing but it was, so far, the
# only way we could have a POSIX compliant method to find if a function
# is defined within this script.
echo " * INFO: Found function $FUNC_NAME"
return 0
fi
won't work because the script is also supposed to work like:
wget --no-check-certificate -O - http://URL_TO_SCRIPT | sudo sh
So, what's the proper, POSIX compliant way to do this?
Plain sh please, no bash, no ksh, no other shell, just plain sh. Oh, and without actually trying to run the function too :)
Is it possible?
I have found a POSIX compliant solution:
if [ "$(command -v $FUNC_NAME)x" != "x" ]; then
echo " * INFO: Found function $FUNC_NAME"
return 0
fi
The question now, Is there a better solution?