Dereferencing variable in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dereferencing variable in loop
# 1  
Old 06-15-2012
Dereferencing variable in loop

Hi ,

I have below code

Quote:

set -x
i=1
while [ $i -le 2 ]
do

eval RC_${i}=102
echo "${RC_$i}"

i=`expr $i + 1`
done >> abc_log

While running above code i am receiving error : bad substitution

I need to use the variable s RC_1, RC_2 and their value outside of the loop.

Can anybody help me on that
# 2  
Old 06-15-2012
correct:
Code:
echo "$RC_${i}"

# 3  
Old 06-15-2012
What's your system? What's your shell?

You can do

Code:
VARNAME="asdf"

echo "${!VARNAME}"

on BASH and some KSH.
# 4  
Old 06-15-2012
Hi,

Its a bash shell..
# 5  
Old 06-15-2012
Have you corrected?
Code:
h5p:/home/vbe $ bash
h5p:/home/vbe $ test001
++ i=1
++ '[' 1 -le 2 ']'
++ eval RC_1=102
+++ RC_1=102
++ echo 1
+++ expr 1 + 1
++ i=2
++ '[' 2 -le 2 ']'
++ eval RC_2=102
+++ RC_2=102
++ echo 2
+++ expr 2 + 1
++ i=3
++ '[' 3 -le 2 ']'

# 6  
Old 06-15-2012
Needs another eval to defer Shell interpreting the line until after the value of $i is known.
Code:
set -x
i=1
while [ $i -le 2 ]
do

eval RC_${i}=102
eval echo "\${RC_$i}"
i=`expr $i + 1`
done >> abc_log

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dereferencing variable inside egrep call

Hi guys I am trying to dereference a variable inside 'egrep -v ' command and getting a 'egrep: syntax error' : $ echo $exclude_list ts584d hf584db for i in `echo $exclude_list`; do egrep -v ${i} my_file done egrep: syntax error egrep: syntax error The syntax of the loop is correct.... (1 Reply)
Discussion started by: aoussenko
1 Replies

2. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

3. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

4. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

5. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

6. Shell Programming and Scripting

perl: dereferencing a hash of hashes

Hi there, I am trying to dereference my hash of hashes but post dereferencing, it seems to lose its structure I am using Data::dumper to help me anaylise. This is the code im using to build the HoH, (data comes from a file). I have also performed a Dumper on the data structure before and after... (1 Reply)
Discussion started by: rethink
1 Replies

7. Shell Programming and Scripting

Dereferencing in PERL

Hi, This should be a simple one. All I am doing is adding an email address to my email. Example abc@xyz.com I understand that the @ means arrays in PERL. So, I coded the backtick (`) to dereference it. But now I get abc`@`xyz.com Your help is appreciated. Thanks Nurani (2 Replies)
Discussion started by: nurani
2 Replies

8. Programming

Dereferencing pointer to incomplete type

// Hello all, I am having this error "Dereferencing pointer to incomplete type " on these 2 lines: xpoint = my_point->x; ypoint = my_point->y; I am having no clue y this is happening. Any help would be greately appreciated!!!! #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: mind@work
2 Replies

9. Shell Programming and Scripting

Dereferencing (?) in Perl

Hi, i want to print the mail exchange servers for a domain using the code below, the problem is that i just get the memory locations (?) of the elements in the output, instead of the mx servers. I really tried to find a solution, but i guess that i just don't get it (objects, OOP etc).. :) ... (2 Replies)
Discussion started by: mjoh
2 Replies

10. Shell Programming and Scripting

variables usage without dereferencing

Hi All, I came across a bit of code that seems to work, even though I didn't expect it to (now that's wierd!) #!/bin/sh set -x nn=15 if then echo "is true" fi The above code ends up comparing the string "nn" to number 15, but still evaluates to true. Here's the output when i run it... (1 Reply)
Discussion started by: ag79
1 Replies
Login or Register to Ask a Question