Variable name change in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable name change in a loop
# 1  
Old 07-13-2010
Variable name change in a loop

I need to do something like this:
Code:
for I in var1 var2 var3 ; do
  $I = "Something calculated inside the loop"
done

Obviously it doesn't work...but is it possible doing that in other ways?

Thanks in advance.
# 2  
Old 07-13-2010
Code:
for I in var1 var2 var3 ; do
  eval $I=\"Something calculated inside the loop\"
done

$ echo $var1
Something calculated inside the loop

# 3  
Old 07-13-2010
Code:
for i in A B C
 do
 j=i ## save it in other variable
 echo $i
 i=XXX
 echo $i
 i=j ## reset back
 done

Output:
A
XXX
B
XXX
C
XXX

# 4  
Old 07-13-2010
Your example, scottn, works fine.
But my real case is a little bit more complicated...here a portion of it:
Code:
for I in UL_LAT UR_LAT LR_LAT LL_LAT UL_LON UR_LON LR_LON LL_LON ; do
            # Convert every value from 71 4 17.92 in 71.071644443
            DEGREES=$( echo ${I} | awk '{ print $1 }' )
            MINUTES=$( echo ${I} | awk '{ print $2 }' )
            SECOND=$(  echo ${I} | awk '{ print $3 }' )
            eval $I=$( echo "scale=9; ${DEGREES}+${MINUTES}/60+${SECOND}/3600" | bc )
        done

Executing that I obtain:
Code:
(standard_in) 1: illegal character: U
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: _
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: T
(standard_in) 1: syntax error
(standard_in) 1: illegal character: U
(standard_in) 1: illegal character: R
(standard_in) 1: illegal character: _
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: T
(standard_in) 1: syntax error
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: R
(standard_in) 1: illegal character: _
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: T
(standard_in) 1: syntax error
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: _
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: T
(standard_in) 1: syntax error
(standard_in) 1: illegal character: U
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: _
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: O
(standard_in) 1: illegal character: N
(standard_in) 1: syntax error
(standard_in) 1: illegal character: U
(standard_in) 1: illegal character: R
(standard_in) 1: illegal character: _
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: O
(standard_in) 1: illegal character: N
(standard_in) 1: syntax error
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: R
(standard_in) 1: illegal character: _
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: O
(standard_in) 1: illegal character: N
(standard_in) 1: syntax error
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: _
(standard_in) 1: illegal character: L
(standard_in) 1: illegal character: O
(standard_in) 1: illegal character: N
(standard_in) 1: syntax error

Any ideas?
# 5  
Old 07-13-2010
You have a few problems, not least this:

Code:
            # Convert every value from 71 4 17.92 in 71.071644443
            DEGREES=$( echo ${I} | awk '{ print $1 }' )
            MINUTES=$( echo ${I} | awk '{ print $2 }' )
            SECOND=$(  echo ${I} | awk '{ print $3 }' )

What does that comment mean, and where do those values come from?

DEGREES, MINUTES and SECOND are not set, so the bc is getting something like
Code:
+/60+/3600

which is obviously junk to it.
# 6  
Old 07-13-2010
"$I" is just the filenames so this is actually being piped to bc:
Code:
UL_LAT+/60+/3600

which is why reading down the right side of the errors gives you the filenames, minus the A, which it can apparently calculate. Learn something new every day.

Try this. It may not be the best approach but see if it works then we can go from there:
Code:
for I in $(cat UL_LAT UR_LAT LR_LAT LL_LAT UL_LON UR_LON LR_LON LL_LON)

# 7  
Old 07-14-2010
@ scottn:
The code I reported is a small piece of a 400 lines bash script, dealing with geographic coordinates manipulation.

In the version not looped, I've something like
Code:
DEGREES=$( echo ${UL_LAT} | awk '{ print $1 }' )
MINUTES=$( echo ${UL_LAT} | awk '{ print $2 }' )
SECOND=$(  echo ${UL_LAT} | awk '{ print $3 }' )
UL_LAT=$( echo "scale=9; ${DEGREES}+${MINUTES}/60+${SECOND}/3600" | bc )
                                                                        
DEGREES=$( echo ${UR_LAT} | awk '{ print $1 }' )
MINUTES=$( echo ${UR_LAT} | awk '{ print $2 }' )
SECOND=$(  echo ${UR_LAT} | awk '{ print $3 }' )
UR_LAT=$( echo "scale=9; ${DEGREES}+${MINUTES}/60+${SECOND}/3600" | bc )

...

and everything works fine. I would like to use a loop to use less lines of code.

@fubaya:
You're right: ${I} is substituted with my variable name and not with the variable content (which is for example "71 4 17.92" as reported in the comment above).
I cannot use "cat" as you suggested because ${I} are variables and not files.
There is a way to fix this?

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -

I solved using "variable indirect references", explained here:
Code:
# Convert every value from 71 4 17.92 in 71.071644443
for I in UL_LAT UR_LAT LR_LAT LL_LAT UL_LON UR_LON LR_LON LL_LON ; do
	# Indirect variable reference (parenthesis mean array)
	eval J=( \$${I} )
	eval ${I}=$( echo "scale=9; ${J[0]}+${J[1]}/60+${J[2]}/3600" | bc )
done

This is the shortest piece of code I can obtain: 4 lines instead of 16.
Thanks for your suggestions!

Last edited by canduc17; 07-14-2010 at 09:15 AM.. Reason: SOLVED!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Change argument in a for loop

In a "for i in *FD.CPY do" loop, I need to change .CPY to .layout so the executed command would be reclay blahFD.CPY >blahFD.layout What do I need to do to modify a copy of i to use after the > symbol? TIA (5 Replies)
Discussion started by: wbport
5 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

Doing a loop to change a value in a file

Hi, I have an N number of files in a directory. I like to write a shell script that would make identical plots for each one of these files. The files have names such as: t00001.dat t00002.dat t00003.dat t00004.dat t00005.dat . . . t00040.dat i.e. the... (4 Replies)
Discussion started by: lost.identity
4 Replies

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

5. Shell Programming and Scripting

Change a field value in a loop

Hi guys, i have an executable file that contains several records and fields. One of the records has a variable filed that must be changed each time i want to execute the file. Would it be possible that i can use a loop to change the value of that field? Suppose that the field address is: Record... (5 Replies)
Discussion started by: saeed.soltani
5 Replies

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

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

8. Shell Programming and Scripting

Change Variable Value from Multiple Scripts and Use these Variable

Hi to All, Please find below details. file_config.config export file1_status="SUCCESS" export file2_status="SUCCESS" file_one.sh I am calling another two shell script from these script. I need to pass individual two script status (If it's "FAILED") to file_main.sh. file_main.sh I... (2 Replies)
Discussion started by: div_Neev
2 Replies

9. Shell Programming and Scripting

Variable value dosent change after the loop changes it

i am new to shell scripting. this is similar to the code which i was writing for some other work. i want the variable 'x' to have the value which it will finally have at the end of the loop ( the number of directories ). but the value of 'x' only changes inside the loop and it remains '0' out-side... (3 Replies)
Discussion started by: chathura666
3 Replies

10. UNIX for Dummies Questions & Answers

change if-else loop to while-do

Hello friends, i wrote a script which includes a couple of if-else loops but i have to change it to while-do loop as it is not allowed to use "break" in if-else loop in bash. #!/bin/bash -x NUM=`find . -name ufsdump_output1.txt | xargs egrep "End-of-tape detected"` if ; then echo "OK!"... (6 Replies)
Discussion started by: EAGL€
6 Replies
Login or Register to Ask a Question