Re-assign variable's value through which FOR LOOP loops

Hi,

I've a requirement where I want to re-assign the value in the variable through which FOR LOOP loops.
For e.g.

 
Snippet of code
---------------
for i in $var
do 
     echo $i >> $tempFile
     var=`echo $another_var | awk -F" " '{print $1}'`
done

I am re-assigning var so that the second time when the FOR LOOP runs it should take the new value? I'm not sure if this or something similar can be done or not?! :confused:. Please suggest.

-dips

$var is supposed to hold a list of values that you will scan with the for loop.

If you want to scan more than 1 list (so if you want $var to hold differents lists of values) you should make it vary into another loop or put your for loop into a function that you will call passing the $var as a parameter.

The code below has not been tested & may be meaning less but it is just an example for demontration purpose of how could the general structure look like if you have few or more list to be assigned to $var :

my_loop(){
tempFile=/whatever/name
for i in $1
do
     echo $i>>$tempFile
done
}

LIST1="a b c d e f g"
LIST2="h i j k l m  n o"

var=${LIST1}
my_loop "$var"

var=${LIST2}
my_loop "$var"

or

find ./ -type d | while read a
do
     var=$(ls $a)

     for i in $var
     do
          echo $i >> $tempFile
     done

done
1 Like

Hi ctsgnb,

Thanks for the head-start!! I'll try this.

-dips