Displaying environment variables dynamically in a Korn Shell Script

Displaying environment variables dynamically in a Korn Shell Script

Post by JHenst » Sat, 07 Sep 2002 05:56:13



If you have an environment variable set up such as:
export env_name="John"

And now you have a script where:

Variable1="env_name"

Is there anyway to get that script to echo the contents of env_name
just using Variable1?  If so, could you give an example of how this is
accomplished?

Thanks,

JH

 
 
 

Displaying environment variables dynamically in a Korn Shell Script

Post by Chris F.A. Johnso » Sat, 07 Sep 2002 06:03:36



> If you have an environment variable set up such as:
> export env_name="John"

> And now you have a script where:

> Variable1="env_name"

> Is there anyway to get that script to echo the contents of env_name
> just using Variable1?  If so, could you give an example of how this is
> accomplished?

       eval echo \$$Variable1

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2002, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Displaying environment variables dynamically in a Korn Shell Script

Post by Andreas K?h?r » Sat, 07 Sep 2002 07:04:31


Submitted by "JHenstay" to comp.unix.shell:

> If you have an environment variable set up such as:
> export env_name="John"

> And now you have a script where:

> Variable1="env_name"

> Is there anyway to get that script to echo the contents of env_name
> just using Variable1?  If so, could you give an example of how this is
> accomplished?

> Thanks,

> JH


[junk] $ env_name="John"
[junk] $ Variable1="env_name"
[junk] $ eval echo $"$Variable1"
John

[junk] $ Variable2='$env_name'
[junk] $ eval echo $Variable2
John

--
Andreas K?h?ri
--------------------------------------------------------------
Stable, secure, clean, free:  www.netbsd.org

 
 
 

Displaying environment variables dynamically in a Korn Shell Script

Post by Dan » Sat, 07 Sep 2002 07:06:06






> > If you have an environment variable set up such as:
> > export env_name="John"

> > And now you have a script where:

> > Variable1="env_name"

<snip>

Without a $ ($env_name)  I think you will always get the literal env_name .
 >

 
 
 

Displaying environment variables dynamically in a Korn Shell Script

Post by Dan » Sat, 07 Sep 2002 07:19:04







> > > If you have an environment variable set up such as:
> > > export env_name="John"

> > > And now you have a script where:

> > > Variable1="env_name"
> <snip>

> Without a $ ($env_name)  I think you will always get the literal env_name
.

Sorry, I read too quick.  I didn't realize you were trying to do indirect
addressing.
 
 
 

Displaying environment variables dynamically in a Korn Shell Script

Post by Stephane CHAZELA » Sat, 07 Sep 2002 19:14:21


[...]
Quote:> [junk] $ env_name="John"
> [junk] $ Variable1="env_name"
> [junk] $ eval echo $"$Variable1"

[...]

Be carefull with that. With bash, $"one string" has a special
meaning (for localization).

You should always quote a '$' when you want it litterrally.

eval echo \"\$$Variable1\"

for example.

--
Stphane

 
 
 

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. P620: xSeries drives in pSeries server

3. KORN shell environment variable question

4. cpp can find, but can not read, <linux/types.h>?

5. korn shell environment variables

6. DOSEMU Disk Image

7. Convert Bash shell script to Korn shell script

8. InPort Adapter

9. hlep with passing variables between scripts in Korn Shell

10. netcfg: TclError: no display name and no $display environment variable

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

12. korn shell variable in variable access

13. no display name and no $DISPLAY environment variable