You are not logged in.
Pages: 1
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
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
[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
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
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
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
No problem
archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson
Offline
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
Pages: 1