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




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #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: 711
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