Variable substitution within a variable name

Variable substitution within a variable name

Post by Robert Johnso » Sun, 17 Dec 2000 07:51:20



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)

 
 
 

Variable substitution within a variable name

Post by Griff Mille » Thu, 21 Dec 2000 00:43:17



> 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}`

Try this instead:
     eval out{$i}=${prn}

- Show quoted text -

Quote:>    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)

--
Griff Miller II
Manager of Information Technology      "Beware of those who seek to win an
Positron Corporation                    argument at the expense of the


 
 
 

1. Q: Variable name within a variable

Feeling very stupid this morning and cant sort this problem out, help
would be appreciated.

Say I have the following sort of script (I know it is not right), its
for illustration purposes.

DIR1=/blob/blobby
DIR2=/blob/notblobby

for EXT in 1 2
do
DIR=$("DIR"$EXT)
echo $DIR
done

Of course, what I would like to see output is

/blob/blobby
/blob/notblobby

In the above example, it shouts bad substitution.  I have tried a number
of quoting variations without luck.
Am I barking completely up the wrong tree, is there another way to do
this?
I need the for loop though as there are other things apart from this
variable I have to manipulate.

Cheers
Aengus

--
----------------------------------------------------------------------

Computational Genome Analysis Laboratory        Tel: +44 (0)171 269 3679
Imperial Cancer Research Fund
Lincoln's Inn Fields, Holborn, London, WC2A 3PX, UK
----------------------------------------------------------------------

2. Linux on Gateway-2000's P75 with Triton, anyone? P90?

3. Expanding variables within variables

4. Newbie needs help...

5. bourne shell variable within a variable

6. what is a kernel?

7. a variable within a variable

8. "encrupted NFS"

9. Cshel: Variables within variables

10. Evaluating a variable within a variable

11. Variable substitution in array name

12. Variable name substitution

13. Variable Name Substitution Problem