Kornshell variable substitution challange!!

Kornshell variable substitution challange!!

Post by Thomas Eliasso » Fri, 09 Jan 1998 04:00:00



Hi!

I'd like this code:

        P="Hello world"
        Q=P
        echo ??

to generate the output "Hello world" where ?? only conatins the
variable Q (i.e NO P:s in the echo argument), how do I do this in ksh?

I've tried ${${Q}} and a whole lot of other things, but I can't
get it working.
Is it possible to solv?

Thanks!

/Thomas

 
 
 

Kornshell variable substitution challange!!

Post by Thomas Eliasso » Fri, 09 Jan 1998 04:00:00


Sorry, I should have been a little more thourough in my description.
The thing is I want a shell script to use the value of an environment
varibel to acees another environment variable.

Maybe it would've been more obvious if I gave the example:
        P_TEXT="Hello world"
        Q=P
        echo ??

I've tried things like ${${Q}_TEXT}
Any suggestions to this?

/Thomas


> Hi!

> I'd like this code:

>         P="Hello world"
>         Q=P
>         echo ??

> to generate the output "Hello world" where ?? only conatins the
> variable Q (i.e NO P:s in the echo argument), how do I do this in ksh?

> I've tried ${${Q}} and a whole lot of other things, but I can't
> get it working.
> Is it possible to solv?

> Thanks!

> /Thomas


 
 
 

Kornshell variable substitution challange!!

Post by Markus Rohn » Fri, 09 Jan 1998 04:00:00



Quote:> Hi!

> I'd like this code:

>    P="Hello world"
>    Q=P
>    echo ??

> to generate the output "Hello world" where ?? only conatins the
> variable Q (i.e NO P:s in the echo argument), how do I do this in ksh?

> I've tried ${${Q}} and a whole lot of other things, but I can't
> get it working.
> Is it possible to solv?

> Thanks!

> /Thomas

   #!/bin/ksh

   P="Hello world"
   Q=$P                   # and NOT Q=P

   print $Q               # or echo $Q but I prefer print

Is that what you want?

Greetings

Markus

--
+----------------------------------------------------------------------------+
| Markus Rohner                             Genossenschaft Migros St.Gallen  
|
| Tel. +41 71 388 26 68                     Informatik                      
|

|
| (NeXTmail/MIME welcome)                   CH-9201 Gossau SG                
|
+----------------------------------------------------------------------------+

 
 
 

Kornshell variable substitution challange!!

Post by Thomas Eliasso » Fri, 09 Jan 1998 04:00:00


Thank you Markus!
If somebody else want to know, here it is!
(got it from Markus in a mail)

#!/bin/ksh
P_TEXT="Hello world" Q=P
eval R='$'${Q}_TEXT
print $R



> > Hi!

> > I'd like this code:

> >       P="Hello world"
> >       Q=P
> >       echo ??

> > to generate the output "Hello world" where ?? only conatins the
> > variable Q (i.e NO P:s in the echo argument), how do I do this in ksh?

> > I've tried ${${Q}} and a whole lot of other things, but I can't
> > get it working.
> > Is it possible to solv?

> > Thanks!

> > /Thomas

>    #!/bin/ksh

>    P="Hello world"
>    Q=$P                   # and NOT Q=P

>    print $Q               # or echo $Q but I prefer print

> Is that what you want?

> Greetings

> Markus

> --
> +----------------------------------------------------------------------------+
> | Markus Rohner                             Genossenschaft Migros St.Gallen
> |
> | Tel. +41 71 388 26 68                     Informatik
> |

> |
> | (NeXTmail/MIME welcome)                   CH-9201 Gossau SG
> |
> +----------------------------------------------------------------------------+

 
 
 

Kornshell variable substitution challange!!

Post by Adam Blai » Fri, 09 Jan 1998 04:00:00



> Hi!

> I'd like this code:

>    P="Hello world"
>    Q=P
>    echo ??

> to generate the output "Hello world" where ?? only conatins the
> variable Q (i.e NO P:s in the echo argument), how do I do this in ksh?

> I've tried ${${Q}} and a whole lot of other things, but I can't
> get it working.
> Is it possible to solv?

> Thanks!

> /Thomas

This worked for me:

p="Hello world"
q=p
eval echo $"$q"

Adam.

 
 
 

Kornshell variable substitution challange!!

Post by Chris J ( » Fri, 09 Jan 1998 04:00:00


Try:
        P="Hello world"
        Q=$P                    <--- Note this line changes
        echo $Q                 <--- then simple echo :)

or...
        P="Hello world"
        Q=P                     <--- This line is as given in question
        eval echo $`echo $Q`    <--- Use eval to evaluate

But why use the last one? The first solution I've given is much simpler...

Chris...



Quote:>Hi!

>I'd like this code:

>    P="Hello world"
>    Q=P
>    echo ??

>to generate the output "Hello world" where ?? only conatins the
>variable Q (i.e NO P:s in the echo argument), how do I do this in ksh?

>I've tried ${${Q}} and a whole lot of other things, but I can't
>get it working.
>Is it possible to solv?

>Thanks!

>/Thomas

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chris Johnson, BEng ESIE IV          | "Let us try to live our lost illusions

http://homepages.shu.ac.uk/~cjohnso0 | we'll never taste the spice of life"
 
 
 

Kornshell variable substitution challange!!

Post by Brian McCaule » Fri, 09 Jan 1998 04:00:00



> > I'd like this code:

> >       P="Hello world"
> >       Q=P
> >       echo ??

> > to generate the output "Hello world" where ?? only conatins the
> > variable Q (i.e NO P:s in the echo argument), how do I do this in ksh?

Surely this is a FAQ?

eval echo \$$Q

I've not tested this in ksh but AFAIK it works in just aboput all
sh-like shells.

--

 .  _\\__[oo       from       | Phones: +44 121 471 3789 (home)

.  l___\\    /~~) /~~[  /   [ | PGP-fp: D7 03 2A 4B D8 3A 05 37...
 # ll  l\\  ~~~~ ~   ~ ~    ~ | http://wcl-l.bham.ac.uk/~bam/

 
 
 

Kornshell variable substitution challange!!

Post by James Youngma » Fri, 09 Jan 1998 04:00:00


  Thomas> Hi!  I'd like this code:

  Thomas>    P="Hello world"
  Thomas>    Q=P
  Thomas>    echo ??

  Thomas> to generate the output "Hello world" where ?? only conatins
  Thomas> the variable Q (i.e NO P:s in the echo argument), how do I
  Thomas> do this in ksh?

  Thomas> I've tried ${${Q}} and a whole lot of other things, but I
  Thomas> can't get it working.  Is it possible to solv?

$ P="hello world"
$ Q=P
$ eval echo \$$Q
hello world      

 
 
 

Kornshell variable substitution challange!!

Post by Kevin Koehl » Sun, 11 Jan 1998 04:00:00


: Hi!
: I'd like this code:
:       P="Hello world"
:       Q=P
:       echo ??
: to generate the output "Hello world" where ?? only conatins the
: variable Q (i.e NO P:s in the echo argument), how do I do this in ksh?
: I've tried ${${Q}} and a whole lot of other things, but I can't
: get it working.
: Is it possible to solv?

You are thinking about:

$ eval echo \$$Q
Hello world

but ksh93 (free for the download) has "reference variables" thus:

$ typeset -n Q=P
$ print $Q
Hello world

-Kevin Koehler

 
 
 

1. Variable substitution within a variable name

Anyone know of a way to use embedded variable names?

i am trying to setup a shell menu for a user to be able to choose a
printer based on their location.  Since their are different numbers of
printers at each location, i am trying to make it dynamic, not static.
I have tried many combinations to no avail so far.

The test script below gets a dynamic list of printers for a particular
store, then should present a menu of those printers for the user to
select. in this case, there are 4 printers so the menu displayed
should be, for example:

1 for printer store032a
2 for printer store032b
3 for printer store032c
4 for printer store032d

Not quite what I am getting.

i=1
lpstat -a | grep store032 | awk '{ print $1 }' |
while read prn
do
   out{$i}=`echo ${prn}`
   echo "$i for printer ${out${i}}"
   i=`expr $i + 1`
done  

Result of running the above script:
printer_names.sh: out1=store032a: not found
printer_names.sh: bad substitution

Anyone have a way to do this, preferably in the bourne shell?

Thanks
--
Robert A. Johnson
Systems Administrator
Cost-U-Less Inc.
8160 304th Ave. SE
Bld. 3 Suite A
Preston, WA 98050
425-222-5022 #161
425-417-2591 (cell)

2. SysAdmin magazine

3. Kornshell: Testing if environment variable exists?

4. Memory and cache-size

5. Substitution Variables

6. How to play avi files on Linux? Any tools? thanks.

7. Variable substitution in a shell script

8. solaris, jni, threads, EPERM

9. sed and variable substitution

10. environment variable for tab substitution

11. variable substitution in sh

12. Variable Substitution Question (KSH88)

13. csh variable substitution mystery