Manera compatible con POSIX para ver si una función está definida en un script sh
Busco la manera correcta de ver si una función está definida o no. Una forma compatible con POSIX.
__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
}
Todo lo anterior se considera bash'isms de acuerdo con los de Debian.Checkbashismos guión.
Tratar de grep la secuencia de comandos también no está bien. por ejemplo:
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
no funcionará porque el script también se supone que funciona como:
wget --no-check-certificate -O - http://URL_TO_SCRIPT | sudo sh
Entonces, ¿cuál es la forma correcta y compatible con POSIX de hacer esto?
Sencillo sh, por favor, no bash, no ksh, no hay otra concha, simplemente sh. Ah, y sin intentar realmente ejecutar la función también :)
¿Es posible?
He encontrado una solución compatible con POSIX:if [ "$(command -v $FUNC_NAME)x" != "x" ]; then
echo " * INFO: Found function $FUNC_NAME"
return 0
fi
La pregunta ahora,¿Hay una mejor solución??