Print variable of a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print variable of a variable
# 1  
Old 04-17-2008
Data Print variable of a variable

Hi,

I'm trying the following:

Code:
code_0_0=1001
code_0_1=Test1
code_1_0=1002
code_1_1=Test1

x=1001

while [ $i -le 1 && $x -ne $code_$i_0 ]
do
i=`expr i + 1`
done

echo The Value Matched at $i variable and has value $code_$i_1

Please let me know where the problem in above code

Regards,
Aravind. C
# 2  
Old 04-18-2008
You want the value of $code_$i_1 where $i is another variable?

Code:
eval echo The value matched at \$i variable and has value \$code_${i}_1

# 3  
Old 04-18-2008
Also, in the increment step, you have missed a $.

i=`expr $i + 1`
# 4  
Old 04-19-2008
Quote:
Originally Posted by aravindc
Hi,

I'm trying the following:

Code:
code_0_0=1001
code_0_1=Test1
code_1_0=1002
code_1_1=Test1

x=1001

while [ $i -le 1 && $x -ne $code_$i_0 ]
do
i=`expr i + 1`
done

echo The Value Matched at $i variable and has value $code_$i_1

Please let me know where the problem in above code

First, you haven't initialized $i.

Then, there are two problems in the test statement. The first is the attempt to use an indirect variable.

If you are using bash, you can build the name of the variable and use the ! expansion:

Code:
iv=code_${i}_0
val=${!iv}

For other shells (and for portability), use eval:

Code:
eval "val=\$code_${i}_0"

In both of the above examples, note the braces around the 'i' to prevent the variable being interpreted as $i_0.

You cannot use && inside a test expression; either use -a or two separate test commands:

Code:
while [ $i -le 1 -a $x -ne $val ]

Or:

Code:
while [ $i -le 1 ] && [ $x -ne $val ]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

2. Shell Programming and Scripting

awk print variable then fields in variable

i have this variable: varT="1--2--3--5" i want to use awk to print field 3 from this variable. i dont want to do the "echo $varT". but here's my awk code: awk -v valA="$varT" "BEGIN {print valA}" this prints the entire line. i feel like i'm so close to getting what i want. i... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

awk print using a variable

hey, just want to ask how to do this. ex. g="hi i am john" h=`echo $g | awk '{print $2}'` echo $h OUTPUT is 'i' What if I want to use a variable instead of using '2', how do I do that? Because this one does not work: a=2 h=`echo $g | awk '{print ${$a}}'` this one also does not... (3 Replies)
Discussion started by: h0ujun
3 Replies

4. Shell Programming and Scripting

Print variable on screen

Hi, I've stored the output of a command into a variable. The variable contains the following output: outputline1 outputline2 outputline3 ... How can I echo the variable so that the output is as follow and not one line: outputline1 outputline2 outputline2 ... Thanks a lot! (4 Replies)
Discussion started by: Sego
4 Replies

5. Shell Programming and Scripting

awk print variable

I have list of files: ls a.pdf b.pdf c.pdf and so on... and I have a file like this: cat file1 apple mango pear and so on... I want to rename my file like this: (7 Replies)
Discussion started by: zorrox
7 Replies

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

7. Shell Programming and Scripting

How to print variable value using file.

I have defined variable in linux using command #year=twenty Now i want to use CGI script to print this value. how can i do so? quick response will be highly appreciated. (6 Replies)
Discussion started by: bhavesh.sapra
6 Replies

8. Shell Programming and Scripting

How to print a value in the variable using awk ?

:b:Hi All, I have a part of a script below: var1="value1" awk 'BEGIN {printf("%36s \n ","value1")}' Instead of directly giving the "value1" , I need to give using "var1" in the above awk statement. Is this possible? If so, what is the modified awk command? Thanks in advance JS (1 Reply)
Discussion started by: jisha
1 Replies

9. Shell Programming and Scripting

print the name of variable

How can I pass the name of the variable inside a for statment I have a script something like below: classA=12 classB=14 classC=16 # classD = 20 all_class="classA classB classC" for i in $all_class do echo $i = $($i) #<---- this one not work #echo $i = $(echo $i) ... (1 Reply)
Discussion started by: sun-guy
1 Replies

10. Shell Programming and Scripting

print variable in awk

i read the variable ph from file and i wanna to print it in awk. example ph=`cat tmpbatch` tail h.txt|grep "| |"|awk -F"|" '{ print "@unpdx.sql",$5 }'"$ph" i try this but it does not work (8 Replies)
Discussion started by: kazanoova2
8 Replies
Login or Register to Ask a Question