Context
I am writting a .sh file which read a .config file. In this .config file (which I can't edit) there are some variables. I want to test if those variables are defined as environment variables.
.config file:
APPLICATION_PATH=/var/www/application
MONGO_DATA_PATH=/var/lib/mongodb
MYSQL_DATA_PATH=/var/lib/mysql
test.sh file:
#!/bin/sh
if test -e ../my_folder/.config # Test if the .config file exists
then
cat ../my_folder/.config | while read line; do # Read the .config file line per line
env_var="${line%=*}" # Get the part before the '=' character of the current line (e.G. APPLICATION_PATH)
echo $env_var # Display the var (e.G. APPLICATION_PATH)
# Here, I would like to display the env var,
# e.G. like it would do using echo $APPLICATION_PATH
# but using the $env_var
( "echo \$$env_var" ) # Throw an error
done
fi
Problem
It seems that ( "echo \$$env_var" )
is not possible. When I run test.sh, it displays this:
APPLICATION_PATH
./test.sh: ligne 13: echo $APPLICATION_PATH : not found
MONGO_DATA_PATH
./test.sh: ligne 13: echo $MONGO_DATA_PATH : not found
MYSQL_DATA_PATH
./test.sh: ligne 13: echo $MYSQL_DATA_PATH : not found
Question
How can I test if there is an environment variable using $env_var
?