How to get value of environment variable in variable name?

How to get value of environment variable in variable name?

Post by avonca.. » Thu, 01 Sep 2005 23:47:51



I have the name of an environment variable in a shell script variable,
and would like to get at the value of that environment variable.  This
happens to be in a ksh script, but I'm looking for a generic solution
that would also work in the standard Bourne shell (i.e, not a ksh
specific solution).  In Perl, this is trivial, but I haven't found the
right incantation of eval or backticks or indirection to get this
working.

For example, if I have an environment variable named BAR whose value is
"foo", and I have a script variable named var whose value is "BAR", how
do I assign "foo" to another variable in the script?

  var=BAR
  value=???

Thanks,
Alfred

 
 
 

How to get value of environment variable in variable name?

Post by Chris F.A. Johnso » Fri, 02 Sep 2005 00:09:01



> I have the name of an environment variable in a shell script variable,
> and would like to get at the value of that environment variable.  This
> happens to be in a ksh script, but I'm looking for a generic solution
> that would also work in the standard Bourne shell (i.e, not a ksh
> specific solution).  In Perl, this is trivial, but I haven't found the
> right incantation of eval or backticks or indirection to get this
> working.

> For example, if I have an environment variable named BAR whose value is
> "foo", and I have a script variable named var whose value is "BAR", how
> do I assign "foo" to another variable in the script?

>   var=BAR
>   value=???

    In any Bourne-type shell:

eval "value=\$$var"

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>

 
 
 

How to get value of environment variable in variable name?

Post by avonca.. » Fri, 02 Sep 2005 00:47:14



>     In any Bourne-type shell:

> eval "value=\$$var"

Thanks!  That works like a charm.   The critical piece I was missing
was the backslash in front of the first $.  I knew the answer would
include an eval, but the various combinations I tried didn't work.

Thanks again,
Alfred

 
 
 

How to get value of environment variable in variable name?

Post by avonca.. » Fri, 02 Sep 2005 02:34:47



>     In any Bourne-type shell:

> eval "value=\$$var"

One more thing.  I'm wondering if there is a way to access the value of
the environment variable without first putting it in another variable.
The original code looked like this:

  if [ "x$SOMEVAR" != "x" ]; then
    ...
  fi

But the SOMEVAR environment variable is now changed to SOMEVAR_${code},
and I can get the value of that environment variable by using the
syntax above, but now the code looks something like this:

  var1=SOMEVAR_${code}
  eval "var2=\$$var1"
  if [ "x$var2" != "x" ]; then
    ...
  fi

And there are lots of these variables and if statements throughout the
code.  Ideally, I would like to have a function that returns the value
of the environment variable that I can use inside the if statement.
Something like this:

  if [ getEnvVarValue SOMEVAR_${code} != "" ]; then
    ...
  fi

I'm not much of a Bourne shell programmer (I'm a Perl weenie), so I may
be missing some obvious things to try, but I usually try to find an
example that does what I want and I haven't been able to find one yet.

Alfred

 
 
 

How to get value of environment variable in variable name?

Post by johngnu » Fri, 02 Sep 2005 02:36:15


Close to the same idea as eval. The echo is in a sub shell. Thus echo
can cost more then an eval ( so I understand ).

myeditor=$(echo "$EDITOR")
echo $myeditor
vi
echo $EDITOR
vi

2 cents. JB

 
 
 

How to get value of environment variable in variable name?

Post by Chris F.A. Johnso » Fri, 02 Sep 2005 02:54:24




>>     In any Bourne-type shell:

>> eval "value=\$$var"

> One more thing.  I'm wondering if there is a way to access the value of
> the environment variable without first putting it in another variable.
> The original code looked like this:

>   if [ "x$SOMEVAR" != "x" ]; then
>     ...
>   fi

> But the SOMEVAR environment variable is now changed to SOMEVAR_${code},
> and I can get the value of that environment variable by using the
> syntax above, but now the code looks something like this:

>   var1=SOMEVAR_${code}
>   eval "var2=\$$var1"
>   if [ "x$var2" != "x" ]; then
>     ...
>   fi

> And there are lots of these variables and if statements throughout the
> code.  Ideally, I would like to have a function that returns the value
> of the environment variable that I can use inside the if statement.
> Something like this:

>   if [ getEnvVarValue SOMEVAR_${code} != "" ]; then
>     ...
>   fi

> I'm not much of a Bourne shell programmer (I'm a Perl weenie), so I may
> be missing some obvious things to try, but I usually try to find an
> example that does what I want and I haven't been able to find one yet.

getvar()
{
   eval "printf '%s\n' \"\$$1\""

Quote:}

if [ -n "`getvar SOMEVAR_${code}`" ]; then

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>

 
 
 

How to get value of environment variable in variable name?

Post by Chris F.A. Johnso » Fri, 02 Sep 2005 02:56:34



> Close to the same idea as eval. The echo is in a sub shell. Thus echo
> can cost more then an eval ( so I understand ).

> myeditor=$(echo "$EDITOR")

   This is exactly the same as:

myeditor=$EDITOR

   What problem are you trying to solve?

Quote:> echo $myeditor
> vi
> echo $EDITOR
> vi

--
    Chris F.A. Johnson                     <http://cfaj.freeshell.org>
    ==================================================================
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
    <http://www.torfree.net/~chris/books/cfaj/ssr.html>
 
 
 

How to get value of environment variable in variable name?

Post by avonca.. » Fri, 02 Sep 2005 04:29:19



> getvar()
> {
>    eval "printf '%s\n' \"\$$1\""
> }

> if [ -n "`getvar SOMEVAR_${code}`" ]; then

Again, this is perfect; just what I needed.  Thanks for all your help,
Chris.  BTW, I just noticed the link to your book.  I'll probably add
it to my next Amazon order.  Like I mentioned in one of my previous
posts, when it comes to Bourne shell programming, I usually pick up
everything I need from examples, and it looks like your book has lots
of good examples.

Thanks,
Alfred

 
 
 

1. c shell script; Howto use environement variables to build environment variables and take their value

In a script I wish to specify two environment variables like this;

setenv FOO bee
setenv ${FOO}_ROOT /home/bee

This is ok and will set "FOO=bee" and "bee_ROOT=/home/bee".

Now I wan't to build the "bee_ROOT" variable using "FOO" and use the
value in a expression, i.e. something like this;

source ${${FOO}_ROOT}/.cshrc

This will not execute. The error is "Variable syntax". It seems that
the shell cannot figure to evaluate the inner ecpression first and
then the outer. I have tried different combination of forcing
evaluation with '' and assigning to variable first etc, but with
little luck.

Any idea/explanation why this is not possible/what I can do instead?

Cheers,
Gert.

2. 2GB File size limitation

3. Value of a variable of which the name is in another variable

4. USR DualStd won't say "bussy"

5. how can I create a single variable that consists of 2 variables' values

6. most appropriate newsgroup

7. detecting difference between unset variable and variable whose value is null in Korn Shell

8. jdk and Readhat CDE compatibility

9. Extract part of value of one variable and fill a second variable

10. variable of variable values

11. Difference between shell variables and environment variables!

12. difference between shell variable and environment variable

13. Setting variables in a variable array of null variables