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

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

Post by Barry Margoli » Fri, 11 Apr 2003 00:17:03





Quote:>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1

>Hi,

>I'm writing a program in bourne shell.

>Now, I want to get the value of a variable of which I know the name
>only at runtime, someone know how can I get this done?

eval value='$'$varname

--

Genuity Managed Services, a Level(3) Company, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

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

Post by Stephane CHAZELA » Fri, 11 Apr 2003 01:09:40


[...]

Quote:> I'm writing a program in bourne shell.

POSIX shell, Bourne shell doesn't have the $(...) construct.

Quote:> Now, I want to get the value of a variable of which I know the name
> only at runtime, someone know how can I get this done?

foo=bar
varname=foo
eval varvalue=\$$varname

the eval command is passed the "varvalue=$foo" string which it
evaluates to set the varvalue variable to the value of the foo
variable.

[...]

Quote:>   URL_SPEC_OPTS=$(echo $URL | sed 's,\:\|\.\|/,_,g')

[...]

easier to read this way:
URL_SPEC_OPTS=$(echo "$URL" | sed 's,[:./],_,g')

or better:

URL_SPEC_OPTS=$(printf %s "$URL" | tr :./ ___)

Or:

IFS=_./:
set -- $URL
eval varvalue=\$"$*"

(this doesn't work in a Bourne shell)

--
Stphane

 
 
 

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

Post by rakesh shar » Fri, 11 Apr 2003 03:45:18



> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1

> Hi,

> I'm writing a program in bourne shell.

> Now, I want to get the value of a variable of which I know the name
> only at runtime, someone know how can I get this done?

> I know that this can be implemented in some other ways, but the code
> will not be so clean and short.

> The interested code follows.

> ...
> URLS=$(cat $CUS_URLS_FILE)
> ...
> for URL in $URLS
>   do
> ...
>   URL_SPEC_OPTS=$(echo $URL | sed 's,\:\|\.\|/,_,g')
>   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   Here I have the name of the variable, but I need its value.

To get the value, you need to bring in the 'eval' command:

VALUE_URL_SPEC_OPTS="`eval echo \\$$URL_SPEC_OPTS`"

 
 
 

1. How to get value of environment variable in variable name?

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

2. Q: mounting devices

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

4. Free shells?---sorry

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

6. OS/2 Boot Manager and Linux problem :\

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

8. Using more file descriptors in Apache 1.3.*

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

10. variable of variable values

11. Q: Variable name within a variable

12. Evaluating variable names stored in variables

13. Variable variable names is csh