Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple variable name resolution 2

Status
Not open for further replies.

Gloups

IS-IT--Management
Sep 16, 2003
394
FR
Hi all,

I have somme different variables sequentialy named like:

VAR1=8
VAR2=9
VAR3=10

i'd like tu deduce my variable name from a resolution of two others like that:

NMBR=1
NAME=VAR

while [ NMBR -ne 10 ]
do
TEST=$NAME$NMBR
echo $TEST
let NMBR=NMBR+1
done


Logically the $TEST value in the 'echo' line is VAR2 for example.
Is there a solution get the value of the concatenated variable in this example i'd like to have 9 (value of VAR2) instead of the name VAR2.

I hope i'm juste as clear as you need :eek:)


Thank's in advance
 
Replace this:
TEST=$NAME$NMBR
By this:
eval TEST=\$NAME$NMBR

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I seem not to be so clear as i want:

if i have VAR1$=MYVALUE

id'like to concatenate two strings like:

part1=VAR
part2=1

to get the complete variable name i'dlike the value. Finaly i want to have the value of VAR1 like $VAR1 by having the value of the value of the two concatenated strings.

and if i understant your way to do i can't get the result i want.
 
Hi Gloups,

I think what you are trying to achieve can be partly solved by using the "one-dimensional array facility" in Korn Shell (and bash) but I don't think it works in Bourne.
eg:
VAR[1]=8
VAR[2]=9
VAR[3]=10

NMBR=1
while [[ NMBR -le 3 ]]
do
echo ${VAR[$NMBR]}
let NMBR=NMBR+1
done

Note: the { } are required in the echo command

I realise this doesn't quite do all you wanted, but when I was looking to do something similar in a script a while ago, this was the best solution I could find. I couldn't force a variable substitution on the contents of a variable.

I hope this helps you.

Mike
 
Perhaps you should consider using an array...
[tt]
set -A AR
AR[1]=8
AR[2]=9
AR[3]=10
NMBR=1
while [ NMBR -le 3 ]
do
TEST=${AR[$NMBR]}
echo $TEST
let NMBR=NMBR+1
done[/tt]
 
My bad, sorry for the typo :~/
Replace this:
TEST=$NAME$NMBR
By this:
eval TEST=\$$NAME$NMBR

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thank'for all

i tried your different ways and that works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top