How to get the values from a variable having variable name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the values from a variable having variable name
# 1  
Old 09-19-2008
How to get the values from a variable having variable name

In shell script how to do the following .......................

For example
I have following 3 variables

VARIABLE_DYNAMIC
JOB_NP_22="ABCD"
JOB_NP_88="HELLO"

based on the value in VARIABLE_DYNAMIC the particular variable value should be displayed
--------------------------------------------------------------
(I) If value of VARIABLE_DYNAMIC="JOB_NP_21"
so by some way i should get value of "JOB_NP_21" i.e ABCD as output

(II) If value of VARIABLE_DYNAMIC="JOB_NP_88"
so by some way i should get value of "JOB_NP_88" i.e HELLO as output

we can retrive variable directly $VARIABLE_DYNAMIC
but as the value in VARIABLE_DYNAMIC is coming dynamically..... i am facing problems in retriving values of the variable (JOB_NP_21 / JOB_NP_88)

Please tell me how to do that

Last edited by anilacharya4u; 09-19-2008 at 06:44 AM..
# 2  
Old 09-19-2008
Code:
eval echo \$$VARIABLE_DYNAMIC

# 3  
Old 09-19-2008
hi anil,

do u want to give the variable as user input or is shud take dynamically?
# 4  
Old 09-19-2008
Data

hi era / subhendu ,

can u please tell me how do i assign the value which i get from this
eval echo \$$VARIABLE_DYNAMIC

to a variable

See the JOB_NP_22 and JOB_NP_88 are actually arrays...
The actual strucure of my variable would be
JOB_NP_22[0]=a,b
JOB_NP_22[1]=c,d

JOB_NP_88[0]=m,n
JOB_NP_88[1]=o,p

i will get these values and loop through the elements ...

Basically the String JOB_NP_22 and JOB_NP_88
will be created partly by "JOB_NP" ( this is hard coded ) and
22 /88 passed as paramters so its a combination of 2

However my hardcode variable loop works
for (( cnt = 0 ; cnt < ${#JOB_NP_22[@]} ; cnt++ ))
do
echo ${JOB_NP_22[cnt]}
done



Need to replace this hardcoded loop and make it dynamic
So please tell me how to assign it to an array and use the array across in my code for looping

Last edited by anilacharya4u; 09-19-2008 at 07:19 AM..
# 5  
Old 09-19-2008
hi anil,

try the following

set -A JOB_NP_22 a,b c,d

while [ $i -lt 2 ]
do
print ${JOB_NP_22[$i]}
((i=i+1))
done


and also do the same for variable
"JOB_NP_88"
# 6  
Old 09-19-2008
Data

Hi,

The array looping for JOB_NP_22 / JOB_NP_88 part is done...

Problem is with the initial part for retriving values i.e
taking value of variable name in VARIABLE_DYNAMIC String


Can the value which we get from this
eval echo \$$VARIABLE_DYNAMIC
be put in array
It is required in the array for loopingthrough elements
# 7  
Old 09-19-2008
anil plz make it clear...so that i will able to ans ur questions...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable values within for loop

Hi All I am trying to fetch the size of three files into three separate variables within a for loop and am doing something like this: for i in ATT1 ATT2 ATT3 do size_$i=`ls -ltr $i | awk '{print $5}'` echo ${size_$i} done but am getting the below error: ksh: size_ATT1=522: not... (3 Replies)
Discussion started by: swasid
3 Replies

2. Shell Programming and Scripting

Subtracting values from variable

Legends, Please help me in , how do i subtract the variable values listed like below. the first value of orig should be subtracted from first value of prev and so on. san> echo $orig 346 316 340 239 410 107 291 139 128 230 167 147 159 159 172 116 110 260 177 0 177 169 168 186 165 366 195... (15 Replies)
Discussion started by: sdosanjh
15 Replies

3. UNIX for Dummies Questions & Answers

Variable is not substituting values

Hi All, OS HPUX 11.11 I am using following script to take controlfile backup. I have used SID variable to hold "ffin1" value, which I again subsitute in "'/db/ffin1/home/oraffin1/$SID_$wdate.ctl'" command. Well, after running this, SID variable does not subsittue it's value, while wdate... (6 Replies)
Discussion started by: alok.behria
6 Replies

4. Shell Programming and Scripting

Assign values to variable

Hi Masters, I want to assign the values of one variable to another variable. Here the varaible name 'var' is dynamic. I know the values of V_2 and U_3, but If the i/p of TYPE is 'U' and the NO is 3, then I want to assign the values of U_3 to var. How we can achieve it? TYPE="U"... (4 Replies)
Discussion started by: ecearund
4 Replies

5. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

6. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

7. UNIX for Dummies Questions & Answers

Dynamic variable values

Bit of a newbie :D with regard to unix scripting and need some advice. Hopefully someone can help with the following: I have a predefined set of variables as follows: AAA_IP_ADD=1.1.1.1 BBB_IP_ADD=2.2.2.2 I have a funnction call which retrieves a value into $SUPPLIER which would be... (3 Replies)
Discussion started by: ronnie_uk
3 Replies

8. Shell Programming and Scripting

comparing values of same variable

Hello all while writing a small script , i got stuck with this simple thing.Hope you guyz can help. Iam trying to read password echo"enter password" read $pwd now i have to check this echo"enter password to proceed" read $pwd now i have to check both the values of the... (2 Replies)
Discussion started by: coolkid
2 Replies

9. Shell Programming and Scripting

getting values from variable in a loop

I have a set of variables: f1="./someFolder" . . f10="./someOtherFolder" And I'm trying to use the following loop for (( i = 0; i <= 10; i++ )) do temp=f$i done I'm trying the get the values from my set of variable to make directories, but I can't seem the get those value... (3 Replies)
Discussion started by: kriuz
3 Replies

10. Shell Programming and Scripting

Substitute variable values

Hi, I am trying to redefine the value of a variable based on another variable value. And I want to read in my variables from a enviroment file in the end -- at least I think so. But 1st here's what I want I need to get working: var_1="11 10 9 8 7 6 5 4 3 2 1" var_2=3 var_3=4 So I want... (12 Replies)
Discussion started by: John Rihn
12 Replies
Login or Register to Ask a Question