How to get an Indirect Variable Value..?


 
Thread Tools Search this Thread
Operating Systems Linux How to get an Indirect Variable Value..?
# 1  
Old 03-12-2010
How to get an Indirect Variable Value..?

Hi,

I've got a small problem.

If varible A stores "B" and Variable B stores C,
How to get the value of variable B by using only Variable A..?

I tried the following but didnt work pease help..

Code:
 
$ var1=vikram
$ echo $var1
vikram
$ vikram=sampath
$ echo $vikram
sampath
$ echo $`echo $var1`
$vikram

In the above code
Why i'm getting "$vikram" when i was expecting "sampath" (because $vikram=sampath)

Please help me the right way to decode in the above situation..

thanks

Vikram shetty
# 2  
Old 03-12-2010
MySQL

See the following code.

Code:
 var1=vikram
 echo $var1
vikram=sampath
 echo $vikram
 eval echo \$$var1

[or]
Code:
 eval echo \$$(echo $var1)

[or]

Code:
   eval echo \$`echo $var1`

# 3  
Old 03-12-2010
Code:
eval result=\$$var1
echo $result

It will print the word sampath
# 4  
Old 03-12-2010
thanks u very much ungalnanbu and selvan..

Its working fine..

Could you please pass on me any material that describes the above behaviour of variables..

thanks

Vikram shetty
# 5  
Old 03-12-2010
Indirect References.
http://www.linuxjournal.com/article/7385
Please go through the above URL.
This will help you to know about variables.

Last edited by thillai_selvan; 03-12-2010 at 02:31 AM..
# 6  
Old 03-12-2010
# 7  
Old 03-12-2010
Thank you very much guys..

warm regards
Vikram Shetty
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern match and replace indirect directory reference using sed

Hi, I need a ksh script to replace indirect directory references in an .ini file with a env variable using sed or awk. The .ini file is for example as such: A=.. B=../ C=../.. D=../../ E=../bin F=../../bin G=../../bin/xml H=../../bin/xml/ Need to replace an instance of .. or... (2 Replies)
Discussion started by: andyatit
2 Replies

2. Shell Programming and Scripting

Indirect variables in Bash

Hello, I've spent hours this morning reading various past forum posts and documentation pages but I can't find exactly what I need. I'm trying to call a variable with a variable in the name without having to make a third variable. For example: path=AB legAB=50 leg$path I want to... (8 Replies)
Discussion started by: DFr0st
8 Replies

3. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

4. Shell Programming and Scripting

Indirect Referral Script

I have a file with two columns of numbers (member IDs): 1 1 2 1 3 1 4 2 5 4 6 1 7 5 8 3 9 2 Think of column 1 as the referee and column 2 as the referrer. Is there a good way to backtrack who referred who? I would like an output, for this example here to be: 1 1 2 1 3 1 4 2 1 (2 Replies)
Discussion started by: mdlloyd7
2 Replies

5. Shell Programming and Scripting

Does SH support indirect expansion like BASH?

Hello, is there a kind soul who can answer me, does the SH support double substitution known as indirect expansion similar to BASH? The syntax for bash is ${!var}. For instance in bash I can write something like this: VAR="value" REF_VAR="VAR" echo ${!REF_VAR} and get the "value"... (1 Reply)
Discussion started by: dimentiy
1 Replies

6. Shell Programming and Scripting

Indirect variable assignment

Hi I have variable A_B=alpha also var1="A" var2="B" I want to retrieve the value alpha using var1 and var2 , somthing like echo ${${var1}_${var2}} that works. Obviously this is receiving syntax error (6 Replies)
Discussion started by: sumir
6 Replies

7. 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

8. Shell Programming and Scripting

Length of an indirect variable

The construct ${#parameter} returns the number of characters in the parameter and ${!parameter} specifies an indirect variable. My question is: How do I combine these two. What I want is ${#!parameter} but this gives an error. Of course I can use: dummy=${!parameter} ${#dummy} but that's a... (0 Replies)
Discussion started by: gone_bush
0 Replies

9. UNIX for Advanced & Expert Users

Compound indirect variable references

Using bash, I'm trying to read a .properties file (name=value pairs), assigning an indirect variable reference for each line in the file. The trick is that a property's value string may contain the name of a property that occurred earlier in the file, and I want the name of the 1st property to... (5 Replies)
Discussion started by: tkrussel
5 Replies
Login or Register to Ask a Question