I have a string of variables to check, and a string of default values like follows:
variables_to_check="name surname address"
variables_default_values="John Doe Paris"
Here is what I would like to do :
- Check if $name is set, if not, give it John as a value
- Check if $surname is set, if not, give it Doe as a value
- ...
Here is the current non-working code I have :
variables_to_check="name surname address"
variables_default_values="John Doe Paris"
i=0
for variable in $variables_to_check
do
((i++))
if [[ -z ${"$variable"+x} ]] #this line doesn't seem to work
#inspired from http://stackoverflow.com/questions/3601515
then
default=$(echo $variables_default_values | cut -d " " -f $i)
set_config $variable $default
declare "$variable=$default" #this doesn't seem to work either
#inspired from http://stackoverflow.com/questions/16553089
fi
done
Any help would be greatly appreciated