Better Way to Get Value of Value of Variable in Bourne shell?

Better Way to Get Value of Value of Variable in Bourne shell?

Post by Hon-Chi » Sun, 29 Jul 2001 10:11:28



Hi

I need to create a Bourne shell function that can change/manipulate the values
of the given variable names, e.g.

  $ P1=John
  $ P2=Jane
  $ P3=Paul

  $ echo "1=$P1, 2=$P2, 3=$P3."
  1=John, 2=Jane, 3=Paul.

  $ add_prefix Hi_ P1 P2 P3

  $ echo "1=$P1, 2=$P2, 3=$P3."
  1=Hi_John, 2=Hi_Jane, 3=Hi_Paul.

After several trial-and-error, I finally got the add_prefix() working, but I
think my solution is too complicated and inefficient, specifically the
"eval $var='$prefix`eval echo \\$$var`'" that takes 2 evals and 1 command
substitution, as shown below.

  add_prefix () {
    prefix=$1
    shift
    for var in $*; do
      eval $var='$prefix`eval echo \\$$var`'
    done
  }

Can any Bourne shell programming guru show me a more concise, efficient and
elegant way in writing the add_prefix() function without using any external
command?

Is there any shorter syntax like ${$var} in Bourne shell that can replace
`eval echo \\$$var` ?  Something concise and simpler will be helpful because
the actual function I need to write is much more complicated than add_prefix()
function shown above.

Thanks.  I appreciate your help.

Regards
Hon-Chi

 
 
 

Better Way to Get Value of Value of Variable in Bourne shell?

Post by Dave Grantie » Sun, 29 Jul 2001 11:21:58


Since you don't have arrays in Bourne (you do in ksh, btw), your
solution is not a bad approach.
If it were me, I would simplify things a bit:
P1="Hi_$P1"
P2="Hi_$P2"
P3="Hi_$P3"
etc.
But I don't know the specifics of your application, so I don't know what
you're trying to do.

Might I suggest using the arrays in ksh?


> Hi

> I need to create a Bourne shell function that can change/manipulate the values
> of the given variable names, e.g.

>   $ P1=John
>   $ P2=Jane
>   $ P3=Paul

>   $ echo "1=$P1, 2=$P2, 3=$P3."
>   1=John, 2=Jane, 3=Paul.

>   $ add_prefix Hi_ P1 P2 P3

>   $ echo "1=$P1, 2=$P2, 3=$P3."
>   1=Hi_John, 2=Hi_Jane, 3=Hi_Paul.

> After several trial-and-error, I finally got the add_prefix() working, but I
> think my solution is too complicated and inefficient, specifically the
> "eval $var='$prefix`eval echo \\$$var`'" that takes 2 evals and 1 command
> substitution, as shown below.

>   add_prefix () {
>     prefix=$1
>     shift
>     for var in $*; do
>       eval $var='$prefix`eval echo \\$$var`'
>     done
>   }

> Can any Bourne shell programming guru show me a more concise, efficient and
> elegant way in writing the add_prefix() function without using any external
> command?

> Is there any shorter syntax like ${$var} in Bourne shell that can replace
> `eval echo \\$$var` ?  Something concise and simpler will be helpful because
> the actual function I need to write is much more complicated than add_prefix()
> function shown above.

--
+---------------------+---------------+


+---------------------+---------------+

 
 
 

Better Way to Get Value of Value of Variable in Bourne shell?

Post by Hon-Chi » Sun, 29 Jul 2001 14:02:50



> Since you don't have arrays in Bourne (you do in ksh, btw), your
> solution is not a bad approach.
> If it were me, I would simplify things a bit:
> P1="Hi_$P1"
> P2="Hi_$P2"
> P3="Hi_$P3"
> etc.

If I can hard-code them, I won't bother trying to write a function to do the
job.  :^)  The actual manipulation of variables I need isn't just simple as
prefixing words.

Quote:

> But I don't know the specifics of your application, so I don't know what
> you're trying to do.

To have a function defined in user's interactive Unix shell (sh, ksh & bash)
that can conditionally manipulate the values of given variables, when invoked
by user.

Quote:

> Might I suggest using the arrays in ksh?

Unfortunately, no, because
a) the function needs to be in Bourne sh since it is the least common
   denominator among sh, ksh & bash.
b) the function is to be used interactively and manipulate variables in
   current shell, hence can't be programmed as external shell script.

Hon-Chi

 
 
 

Better Way to Get Value of Value of Variable in Bourne shell?

Post by t.. » Sun, 29 Jul 2001 16:27:13



>   add_prefix () {
>     prefix=$1
>     shift
>     for var in $*; do
>       eval $var='$prefix`eval echo \\$$var`'
>     done
>   }

The echo, command substitution and second eval are unnecessary
(and potentially dangerous with special characters).
All you need is

       eval "$var=$prefix\$$var"

Note double quotes.

--
Tapani Tarvainen

 
 
 

Better Way to Get Value of Value of Variable in Bourne shell?

Post by Dave Grantie » Mon, 30 Jul 2001 20:53:00


Ah well, just wondering.
My advice is to stick with what works (ie, what you have).

Best of luck!
Dave



> > Might I suggest using the arrays in ksh?

> Unfortunately, no, because
> a) the function needs to be in Bourne sh since it is the least common
>    denominator among sh, ksh & bash.
> b) the function is to be used interactively and manipulate variables in
>    current shell, hence can't be programmed as external shell script.

--
+---------------------+---------------+


+---------------------+---------------+
 
 
 

Better Way to Get Value of Value of Variable in Bourne shell?

Post by Hon-Chi » Tue, 31 Jul 2001 11:58:17



> The echo, command substitution and second eval are unnecessary
> (and potentially dangerous with special characters).
> All you need is

>        eval "$var=$prefix\$$var"

> Note double quotes.

Thanks.  This is the improvement over my original code that I was looking for.

Hon-Chi

Disclaimer: BTW, for those of you who are curious, besides asking a somewhat
similar question as mine on the same day and also using Google Groups, the

each other, we aren't taking same class, nor attending same school, nor being
assigned same homework/quiz, nor we work for the same company.  :^)