Modo compatível com POSIX para ver se uma função é definida em um script sh
Eu estou atrás da maneira correta de ver se uma função é definida ou não. Uma maneira compatível com 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
}
Todos os itens acima são considerados bash'isms de acordo com o debiancheckbashisms roteiro.
Tentando grep o script também não está ok. por exemplo:
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
não funcionará porque o script também deve funcionar como:
wget --no-check-certificate -O - http://URL_TO_SCRIPT | sudo sh
Então, qual é a maneira correta e compatível com POSIX de fazer isso?
Sh simples por favor, não bash, não ksh, nenhum outro shell, simplesmente sh. Ah, e sem realmente tentar executar a função também :)
É possível?
Eu encontrei uma solução compatível com POSIX:if [ "$(command -v $FUNC_NAME)x" != "x" ]; then
echo " * INFO: Found function $FUNC_NAME"
return 0
fi
A questão agoraExiste uma solução melhor?