Dynamic variable values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Dynamic variable values
# 1  
Old 07-11-2008
Dynamic variable values

Bit of a newbie Smilie 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 used as a prefix to obtain the IP address for a specific SUPPLIER. The function works and returns me a value but for explanation purposes it could be defined as follows:

$SUPPLIER=BBB

I need to now retreive the value from AAA_IP_ADD or BBB_IP_ADD based on the setting for $SUPPLIER.

So I concatenate '_IP_ADD' onto $SUPPLIER. To try and make sense of it here is what I am trying to run:

echo $SUPPLIER'_IP_ADD'

This returns a literal value and not the value stored in AAA_IP_ADD or BBB_IP_ADD

Can anyone advise how I get the actual value for these variables.

Thanks

Last edited by ronnie_uk; 07-11-2008 at 08:43 AM..
# 2  
Old 07-11-2008
One way ...
Code:
AAA_IP_ADD="1.1.1.1"
BBB_IP_ADD="2.2.2.2"

SUPPLIER="BBB"

eval IPADDR='$'{${SUPPLIER}_IP_ADD}
echo $IPADDR

# 3  
Old 07-11-2008
Fantastic, thank you very much.
# 4  
Old 07-11-2008
BTW, if you are using ksh93, another way to do what you want to do is to use a nameref i.e.
Code:
#!/bin/ksh93

AAA_IP_ADD="1.1.1.1"
BBB_IP_ADD="2.2.2.2"

SUPPLIER="BBB"

nameref IPADDR=${SUPPLIER}_IP_ADD
print $IPADDR

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut command with dynamic passing of delimiter and position values

Hi All, We have a requirement of picking nth position value by using cut command. value would be delimited by any symbols. We have to pass delimited value and postition to get the value in a string. ex. echo "A,B,C,D,E" |cut -d "," -f3 echo "A|B|C|D|E"|cut -d "|" -f2 Kindly frame the... (5 Replies)
Discussion started by: KK230689
5 Replies

2. Shell Programming and Scripting

Replace sql with dynamic values

Hi Guys, I am using a function to replace the values dynamically to frame sql query by reading a file. My file will have column names like file.txt col_1 col_2 expected output: select id,col_1,col_2 from ( select a.id, a.col_1, rank() over (ORDER BY cast(a.col_1 AS double)... (5 Replies)
Discussion started by: Master_Mind
5 Replies

3. Shell Programming and Scripting

How to lowercase the values in a column in awk and include a dynamic counter?

Hi, I am trying to incorporate 2 functions into my `awk` command. I want to lower case Column 2 (which is essentially the same information in Col1, except in Col1 I want to maintain the capitalization) and I want to count from 0-N that begins and ends with the start of certain markers that I... (6 Replies)
Discussion started by: owwow14
6 Replies

4. Shell Programming and Scripting

Passing dynamic variable within another variable.

I have a small program which needs to pass variable dynamically to form the name of a second variable whose value wil be passed on to a third variable. ***************** Program Start ****************** LOC1=/loc1 PAT1IN=/loc2 PAT2IN=/loc3 if ; then for fpattern in `cat... (5 Replies)
Discussion started by: Cyril Jos
5 Replies

5. Shell Programming and Scripting

Using dynamic arrays to extract the values

Hi all, We have requirement to generate load timing based on subject areas HOUSEHOLD, BANKING and TRADING. These values are stored in an array SUB_ARR SUB_ARR=("HOUSEHOLD" "BANKING" "TRADING") Based on indicator files produced while processing data for each type, we need to get the stats (using... (2 Replies)
Discussion started by: sanjaydubey2006
2 Replies

6. Shell Programming and Scripting

dynamic values in a row

hi i have an input file in which there are diffrent values for xxxx,yyyyyy,zzzzzzz how can i arrange the dynamic values of x,y&z in a row. input file: xxxxx 1 yyyyyy 4 yyyyyy 5 zzzzzzzz 7 yyyyyy 13 zzzzzzzz 7 zzzzzzzz 6 yyyyyy 14 yyyyyy 12 zzzzzzzz 4 yyyyyy 4 yyyyyy 5 yyyyyy 6... (6 Replies)
Discussion started by: dodasajan
6 Replies

7. Shell Programming and Scripting

dynamic variable name

I found one post in another site with a solution for my problem the below solution should explain what I want. #!/bin/sh first="one" second="two" third="three" myvar="first" echo ${!myvar} But this gives error 'bad substitution' System info SunOS sundev2 5.9... (3 Replies)
Discussion started by: johnbach
3 Replies

8. Shell Programming and Scripting

Assigning values to reference variables for a dynamic menu driven script.

How do I assign values to reference variables? I am assigning a variable name to --> $user_var Then I am trying to change its underlying variable value by $((user_var))=$user_value .. its failing,, Please let me know if there is a way to do this dynamically.. FileA.props... (5 Replies)
Discussion started by: kchinnam
5 Replies

9. Shell Programming and Scripting

Assigning values for a dynamic array for an input

Hello, Can somebody please give me a snippet for the below requirement. I want to assign the values separeted by a comma to be assigned to a dynamic array. If I give an input (read statement) like abc1,abc2,abc3,abc4,abc5, all these strings abc* should be assigned to an array like below... (2 Replies)
Discussion started by: suneelj
2 Replies

10. Shell Programming and Scripting

Extracting dynamic values

Hi, I am stuck with extracting values by combining 2 dynamically extracted values. The code goes like this #!/usr/bin/ksh ID1="abcd" i=1 #this is a dynamic value and keeps on changing b="ID" #this is static now i want the value of ID1 variable. like echo $b$i But echo... (1 Reply)
Discussion started by: chaitanyapn
1 Replies
Login or Register to Ask a Question