You are not logged in.

#1 2008-10-24 17:29:23

szypul
Member
From: Poland
Registered: 2007-09-07
Posts: 16

[SOLVED]bash problem

Hi,
I'm passing name of env. variable as an scrip argument (. ./script.sh NAME). I would like to check it's value inside my script. How to do it? ${1} gives me name, how can I get value behind this name?

Last edited by szypul (2008-10-31 11:26:42)


The only way to discover the limits of the possible is to go beyond them into the impossible.

Offline

#2 2008-10-24 18:51:58

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: [SOLVED]bash problem

If you defined NAME in the parent environment, it will be passed to script.sh as would any other variable.

For instance, if script.sh was this:

#!/bin/sh
#script.sh
echo "Inside script.sh, NAME=$NAME"

The following would work:

$ NAME="Barack Obama"
$ ./script.sh
Inside script.sh, NAME=Barack Obama

Your example would not work, as it does nothing with the environment variable, but passes the name of the variable as an argument.

Offline

#3 2008-10-24 19:13:24

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: [SOLVED]bash problem

[randy@voodoo ~]$ cat test.sh
#!/bin/bash
eval echo "\$$1"
[randy@voodoo ~]$

produces

[randy@voodoo ~]$ sh test.sh SHELL
/bin/zsh
[randy@voodoo ~]$ sh test.sh TERM
xterm
[randy@voodoo ~]$ sh test.sh USER
randy
[randy@voodoo ~]$

Last edited by rson451 (2008-10-24 19:14:25)


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#4 2008-10-24 19:18:38

szypul
Member
From: Poland
Registered: 2007-09-07
Posts: 16

Re: [SOLVED]bash problem

phrakture, I think you missunderstood me. In shell I can do "echo $NAME" to get its value. I would like to get that value inside my script.
Edit:
Thaks rson451: ) Too simple to think about it: )

Last edited by szypul (2008-10-24 19:27:32)


The only way to discover the limits of the possible is to go beyond them into the impossible.

Offline

#5 2008-10-24 19:24:42

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: [SOLVED]bash problem

ok..

[randy@voodoo ~]$ cat test.sh
#!/bin/bash
TEST=$(eval echo "\$$1")
echo "TEST=$TEST"

produces

[randy@voodoo ~]$ sh test.sh SHELL
TEST=/bin/zsh
[randy@voodoo ~]$ sh test.sh USER
TEST=randy
[randy@voodoo ~]$

Last edited by rson451 (2008-10-24 19:25:16)


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#6 2008-10-24 20:17:18

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: [SOLVED]bash problem

szypul wrote:

phrakture, I think you missunderstood me. In shell I can do "echo $NAME" to get its value. I would like to get that value inside my script.

Yeah, sorry I misread 8)

Offline

#7 2008-10-24 20:31:12

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: [SOLVED]bash problem

No problem smile


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#8 2008-10-25 10:10:54

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: [SOLVED]bash problem

You can do this easier and cleaner

$ var_name=i_am_a_var
$ i_am_a_var=i_am_the_value
$ echo ${!var_name}
i_am_the_value

< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

Board footer

Powered by FluxBB