Increment nested for loop parllely


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Increment nested for loop parllely
# 1  
Old 09-01-2008
Data Increment nested for loop parllely

Hi ,

I am trying to increment the nested for loops parellely,but i cant ,i used continue 2 but the second loop not getting increment.

no1="1 6 5 4 8"
no2="4 7 8 0 1"
for var1 in $no1 ; do
for var2 in $no2 ; do
line1
line 2
line 3
continue 2
done
done

Please help on this
# 2  
Old 09-01-2008
Hi.

It is not clear what your goal is. In your construction, every time you enter the inner loop, the sequence will begin again -- that's the nature of the loop.

Here's how I would do this, but it is guessing what you are trying to accomplish:
Code:
#!/bin/bash -

# @(#) s2       Demonstrate array use for processing in parallel.

# See http://www.tldp.org/LDP/abs/html/arrays.html for details on
# arrays.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
set -o nounset

echo
echo " Results:"

no1=( 1 6 5 4 8 )
no2=( 4 7 8 0 1 )
length=${#no1[*]}
echo " length is $length"

i=0
while (( i < length ))
do
  echo iteration $i no1[$i] = ${no1[$i]}, no2[$i] = ${no2[$i]}
  (( i++ ))
done

exit 0

Producing:
Code:
% ./s2

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0

 Results:
 length is 5
iteration 0 no1[0] = 1, no2[0] = 4
iteration 1 no1[1] = 6, no2[1] = 7
iteration 2 no1[2] = 5, no2[2] = 8
iteration 3 no1[3] = 4, no2[3] = 0
iteration 4 no1[4] = 8, no2[4] = 1

See Arrays ... cheers, drl
# 3  
Old 09-03-2008
Bug

Hi,

Thanks for this and sorry for not explaining my goal clearly.
This is what i tried to post and i got it exactly and its working fine.
Thanks again for this help.
# 4  
Old 09-03-2008
A construct which works even in Bourne Classic (tm) is to run the variables in parallel in a while loop, like this:

Code:
while read var1 var2; do
  : stuff
done <<HERE
1 4
6 7
5 8
4 0
8 1
HERE

# 5  
Old 09-03-2008
Bug

Thanks for this...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parallel increment of nested for loop

Hi, I am using solaris 5.10 environment and need help on doing parallel increment of nested for loop. Samples #inside the code the values assigned to a variable by another awk command will be like a=/xyz/pg/as /xyz/pg/as2 /xyz/pg/as3 b=/xyz/sd/fd1 /xyz/sd/fd2 /xyz/sd/fd3 for q in... (1 Reply)
Discussion started by: ananan
1 Replies

2. Windows & DOS: Issues & Discussions

Batch file loop and increment value for condition

I am trying to have the below batch file do following two things: 1. only allow the values YES,yes,Y,y, or NO,no,N,n 2. increment the counter %var1 only if answer to question 2 is "y" and not able to get the syntax correct. If %var1%=1 then I am trying to display function :end. Thank you :).... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

Find and increment at each occurence of string (loop)

I created script (sh shell) to generate vlc playlist based on some data files. All works fine so far except one string I do not know how to handle with. VLCSTART='<vlc:id>' VLCV=0 VLCEND='</vlc:id>' echo -e $'\n'$'\t'$'\t'$'\t'$'\t'\$VLCSTART$VLCV$VLCENDOutput file contains several occurences... (10 Replies)
Discussion started by: TiedCone
10 Replies

4. Shell Programming and Scripting

For/While Loop to Increment Filenames in a Script

Daily stupid question. I want to increment the file name everytime the script is run. So for example if the filename is manager.log and I run the script, I want the next sequence to be manager.log1. So to be clear I only want it to increment when the script is executed. So ./script... (10 Replies)
Discussion started by: metallica1973
10 Replies

5. Shell Programming and Scripting

Nested if loop

Hi Team, I just want to check whether my nested if loop used is correct or not. if ] if ] export1 else export2 fi else if ] export3 else export4 fi fi Thanks Shiva (5 Replies)
Discussion started by: shivashankar_S
5 Replies

6. Programming

[Xquery] How to do a increment in a For loop

Hello men. How can i build a simple increment for $a by Xquery such as ? let $a := 0 for $i in (1 to 10) let $a := $a + 1 return $a why a in this loop always is '1' Thank you for reading, its will really helpful for my job if i can solve this problem :D:D (3 Replies)
Discussion started by: tien86
3 Replies

7. Shell Programming and Scripting

the given code goes in infinite loop and does not increment variable i

code is as #!/bin/sh i=1; while do welcome $i times; i='expr $i+1'; done exit 0; (6 Replies)
Discussion started by: mrityunjay22
6 Replies

8. Shell Programming and Scripting

Increment date in 'for' loop?

Hi Guys, My first post..:) Right...I want to move existing files (with some date in their name) currently in $mainftp, to $mainfolder/$foldate/system1. I'd like to be able to increment date in the for loop? Is this possible or should I use a different technique. The script return the... (4 Replies)
Discussion started by: SunnyK
4 Replies

9. Shell Programming and Scripting

nested loop

I have two do loops. When I break of the inner loop it doesn't go back to the outer loop but exit the program. (5 Replies)
Discussion started by: chinog
5 Replies
Login or Register to Ask a Question