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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Re-assign variable's value through which FOR LOOP loops
# 1  
Old 01-17-2011
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.

Code:
 
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?! Smilie. Please suggest.

-dips
# 2  
Old 01-17-2011
$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 :

Code:
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
Code:
find ./ -type d | while read a
do
     var=$(ls $a)

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

done

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 01-19-2011
Hi ctsgnb,

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

-dips
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

2. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

3. Shell Programming and Scripting

Assign Values to a Variable in While Loop and Update the File

Hello, Could anyone please help me with Assigning a value to variable and then updating the value in the original file IFS='|' while read -r Serial_ID JOB_NAME STATUS do if then echo "Perform Fuctions" ???Assign STATUS to COMPLETED and Update File??? done <File (7 Replies)
Discussion started by: infernalhell
7 Replies

4. Shell Programming and Scripting

Do While Loop + Read From File + assign line to a variable

Hello, I am using below code for reading from a file and assigning the values to a variable , but it is loosing the value after the loop , please suggest to retain the value of the variable after the loop , while IFS=: read -r line do set $dsc=$line echo 'printing line variable ' $line... (1 Reply)
Discussion started by: ParthThakkar
1 Replies

5. Shell Programming and Scripting

Assign variable name through loop

Hi As bash does not support multidimensional arrays (?), I need some help with a problem. What I want to do is to assign variable names containing a counter in a loop . what I want to do is basically something like this: #!/bin/bash for i in {1..8}; do var$i = "some command" done... (6 Replies)
Discussion started by: Tobbev
6 Replies

6. UNIX for Advanced & Expert Users

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the values... (12 Replies)
Discussion started by: manishab00
12 Replies

7. Fedora

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the... (1 Reply)
Discussion started by: manishab00
1 Replies

8. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

9. Shell Programming and Scripting

Is it possible to re-assign the variable form the loop?

Good day, everyone! I've got a small problem with re-assigning the variable form the loop. What I'm trying to do is: #!/bin/bash/ VAR1="AAA" VAR2="BBB" VAR3="CCC" for WORD in VAR1 VAR2 VAR3; do $WORD="DDD" echo $WORD done :o That's the output and error messages: -bash:... (2 Replies)
Discussion started by: Nafanja
2 Replies

10. Shell Programming and Scripting

for loops can i loop in hundreds?

I have the following for ((i=100; i<=1000; i++)) do this goes in increments of 1 I need it to go up in increments of 100, how is that done in a for loop? (1 Reply)
Discussion started by: gazz1982
1 Replies
Login or Register to Ask a Question