Nested UNIX variables with eval
eval is a nice function and particularly useful to resolve the value of nested unix variables properly. Here is a scenario.
You have 3 apache servers that belong to different environments.
server_qa_apache=1.1.1.1
server_test_apache=2.2.2.2
server_production_apache=3.3.3.3
Assume you pass the environment as an input variable and want to obtain the server IP.
Environment can be qa, test or production.
ENV=$1
SERVER_IP=${server_$ENV_apache}
-bash: ${server_$ENV_apache}SERVER_IP=${server_$ENV_apache}: bad substitution
The above line will report the error because of nested unix variable.
Use a backslash for nested unix variables.
echo \${server_${ENV}_apache}
The above line will output the variable name similar to
${server_production_apache}
To resolve the value of the variable correctly, you can use eval
eval SERVER_IP=\${server_${ENV}_apache}
echo $SERVER_IP
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home