Needed: a variable who's name is controlled by a variable

Needed: a variable who's name is controlled by a variable

Post by Bill Irw » Tue, 10 Jan 1995 09:13:44



Here's a cute one that has me stumped.

user1=x1
user2=x2
for count in 1 2
do
    echo "User $count is: $user\{$count\}"
done

This obviously doesn't work and what I want for the output is:

User 1 is: x1
User 2 is: x2

Either Bourne or Korne is fine.
--
Bill Irwin     -      The Westrheim Group     -    Vancouver, BC, Canada
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
uunet!twg!bill              604-431-9600 (voice) |     Your Computer  

 
 
 

Needed: a variable who's name is controlled by a variable

Post by Bill Irw » Wed, 11 Jan 1995 02:13:05


|Here's a cute one that has me stumped.

|user1=x1
|user2=x2
|for count in 1 2
|do
|    echo "User $count is: $user\{$count\}"
|done

|This obviously doesn't work and what I want for the output is:

|User 1 is: x1
|User 2 is: x2

|Either Bourne or Korne is fine.

I got the solution from Siegfried Leisen, e-mail:

| eval echo "User $count is: \$user$count"

This did the trick.
--
Bill Irwin     -      The Westrheim Group     -    Vancouver, BC, Canada
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
uunet!twg!bill              604-431-9600 (voice) |     Your Computer  


 
 
 

Needed: a variable who's name is controlled by a variable

Post by Cameron Newh » Wed, 11 Jan 1995 19:04:15



>Here's a cute one that has me stumped.
>user1=x1
>user2=x2
>for count in 1 2
>do
>    echo "User $count is: $user\{$count\}"
>done
>This obviously doesn't work and what I want for the output is:
>User 1 is: x1
>User 2 is: x2

Try this:

user1=x1
user2=x2
for count in 1 2
do
    echo "User $count is: "$(eval echo $"user$count")
done

--
----------------------------------------------------------------------------
Cameron Newham   |  Everything counts


 
 
 

Needed: a variable who's name is controlled by a variable

Post by Jan de Rij » Wed, 11 Jan 1995 21:28:34


: Here's a cute one that has me stumped.

: user1=x1
: user2=x2
: for count in 1 2
: do
:     echo "User $count is: $user\{$count\}"
-> eval echo "User $count is: \$user$count"
: done

: This obviously doesn't work and what I want for the output is:

: User 1 is: x1
: User 2 is: x2

: Either Bourne or Korne is fine.
: --
: Bill Irwin     -      The Westrheim Group     -    Vancouver, BC, Canada
: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: uunet!twg!bill              604-431-9600 (voice) |     Your Computer  

---
--
Jan de Rijke                           |  FORWARD IN ALL DIRECTIONS !
BIM                                    |             (3 Mustaphas 3)
Kwikstraat 4, 3078 Everberg, BELGIUM   |

 
 
 

Needed: a variable who's name is controlled by a variable

Post by Horst Scheuerma » Sat, 14 Jan 1995 21:28:41



Quote:> Here's a cute one that has me stumped.
> user1=x1
> user2=x2
> for count in 1 2
> do
>     echo "User $count is: $user\{$count\}"

      eval dummy=\$user$count
      echo "user $count is $dummy"

> done
> This obviously doesn't work and what I want for the output is:
> User 1 is: x1
> User 2 is: x2
> Either Bourne or Korne is fine.
> --
> Bill Irwin     -      The Westrheim Group     -    Vancouver, BC, Canada
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> uunet!twg!bill              604-431-9600 (voice) |     Your Computer  


--
Horst Scheuermann       Universitaets-Rechenzentrum Trier       __o
Universitaetsring 19    D-54286 Trier                         _`\<,_
Telefon: 0651 201 3436  Telefax: 0651 201 3921               (_)/ (_)