I need to be able to check if a variety of similar variables are set in bash.
I need to check if the string defined in the variable CONFIG_STRING_TO_CHECK has been set.
I would like to do something like this:
#!/bin/bash
CUSTOM_PREFIX='custom1'
# Common Variable Name Endings:
CONFIG_STRINGS=( "config1" "config2" )
# Loop over common variable endings
for CONFIG_STRING in "${CONFIG_STRINGS[@]}" do :
# Set the variable name to check
CONFIG_STRING_TO_CHECK="${CUSTOM_PREFIX}_$CONFIG_STRING"
# Check if variable is defined
if [ -z ${CONFIG_STRING_TO_CHECK+x} ]; then
echo "$CONFIG_STRING_TO_CHECK is declared";
else
echo "$CONFIG_STRING_TO_CHECK is not declared";
exit 1;
fi
done
After some Googling I found this answer, but it doesn't work. I think it's because this is checking if the variable CONFIG_STRING_TO_CHECK is set... which it always is.
Just to be clear, I would like this to check if the following strings are set:
custom1_config1
custom1_config2
Not:
CONFIG_STRING_TO_CHECK
I hope this makes sense. Pease help.