Muchas gracias a "Gavin Smith" en stackoverflow.com por la primera parte de este consejo.
Expresiones como ${${a}} no funcionan. Para evitarlo, puede usar eval:
b=value a=b eval aval=\$$a echo $aval
La salida es
value
¡Gracias por el consejo Gavin!
Vale, entonces, ¿por qué querrías hacerlo?
Para mí, necesitaba probar que ciertas variables se estaban configurando desde un archivo de configuración incluido. Usando un bucle y la técnica de anidamiento que se muestra arriba, podemos tomar:
esto:
if [ "${KIDFILE}" == "" ] ; then
echo "ERROR - KIDFILE Not Set"
exit 1
fi
if [ "${KUSER}" == "" ] ; then
echo "ERROR - KUSER Not Set"
exit 1
fi
if [ "${KSERVER}" == "" ] ; then
echo "ERROR - KSERVER Not Set"
exit 1
fi
if [ "${KRFILE}" == "" ] ; then
echo "ERROR - KRFILE Not Set"
exit 1
fi
if [ "${KLFILE}" == "" ] ; then
echo "ERROR - KLFILE Not Set"
exit 1
fi
if [ "${KLINK}" == "" ] ; then
echo "ERROR - KLINK Not Set"
exit 1
fi Y reemplázalo por el más elegante:
NEEDVARS="KIDFILE KUSER KSERVER KRFILE KLFILE KLINK"
for MYVAR in ${NEEDVARS}; do
eval MYVAL=\$${MYVAR}
if [ "${MYVAL}" = "" ] ; then
echo "${MYVAR} NOT SET"
exit 1
fi
done