Unix Blog

Monday, October 25, 2010

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

posted by Jayanthi Krishnamurthy @ 9:18 AM   0 Comments

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

Newer›  ‹Older

About Me

Name: Jayanthi Krishnamurthy

View my complete profile

Posts

  • Nested UNIX variables with eval

Older Posts Newer Posts

Archives

Subscribe to
Posts [Atom]