The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
nested loop problem mmunir Shell Programming and Scripting 5 07-04-2008 03:16 AM
the given code goes in infinite loop and does not increment variable i mrityunjay22 Shell Programming and Scripting 6 12-26-2007 02:20 AM
Increment date in 'for' loop? SunnyK Shell Programming and Scripting 4 10-30-2007 06:12 AM
Variable in While Loop Nested If geass Shell Programming and Scripting 6 03-26-2007 06:09 PM
nested loop chinog Shell Programming and Scripting 5 04-20-2005 10:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-01-2008
nmahendran nmahendran is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 4
Unhappy 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 (permalink)  
Old 09-01-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 699
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 (permalink)  
Old 09-03-2008
nmahendran nmahendran is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 4
Smile

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 (permalink)  
Old 09-03-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 09-03-2008
nmahendran nmahendran is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 4
Smile

Thanks for this...
Sponsored Links
Closed Thread

Bookmarks

Tags
increment nested for loop

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0