On Wed, 2003-09-24 at 22:13, Bill Gradwohl wrote: > I'm trying to read a file into an array and not having much success. > > The following test script is self contained. > > #!/bin/bash > > # Create a test file to read > cat >/tmp/junkfile <<-"CODE" > line 1 > line 2 > line 3 > CODE > > echo ===========Loop 1============== > declare -a RULES > i=0 > j=4 > > # Read the test file line by line into the RULES array. > cat /tmp/junkfile| while read x; do > RULES[i++]="$x" > # Show what RULES contains > echo j=$j i=$i RULES has ${#RULES[*]} elements. ${RULES[*]} > done > echo j=$j i=$i RULES has ${#RULES[*]} elements. ${RULES[*]} > > > echo ===========Loop 2============== > declare -a rules > i=0 > j=4 > > while test $i -lt 3; do > rules[i++]="line $((i+1))" > # Show what rules contains > echo j=$j i=$i rules has ${#rules[*]} elements. ${rules[*]} > done > echo j=$j i=$i rules has ${#rules[*]} elements. ${rules[*]} > > rm /tmp/junkfile > > /////////////////////////////////////////////////////////////////////// > If you run it (its harmless) you'll get the following output: > > ===========Loop 1============== > j=4 i=1 RULES has 1 elements. line 1 > j=4 i=2 RULES has 2 elements. line 1 line 2 > j=4 i=3 RULES has 3 elements. line 1 line 2 line 3 > j=4 i=0 RULES has 0 elements. > ===========Loop 2============== > j=4 i=1 rules has 1 elements. line 1 > j=4 i=2 rules has 2 elements. line 1 line 2 > j=4 i=3 rules has 3 elements. line 1 line 2 line 3 > j=4 i=3 rules has 3 elements. line 1 line 2 line 3 > > The first loop appears to stuff the array with values and i is steadily increasing. > After the loop ends, i is 0 and the array is empty. Why? > Since i & j are initialized prior to loop start, and displayed during the loop, why is j's value maintained across the loop and i appears to get reinitialized to the pre loop value? > > The second loop is similar. Again j keeps its value the entire time, and i gets increased steadily. At loop end both j and i are what I would expect them to be. > > How are these two loops different? It has to have something to do with the read statement but I can't figure it out. > > Thank You > > Bill Gradwohl > (817) 224-9400 x211 > www.ycc.com > SPAMstomper Protected Email I haven't spent much time looking at this, but it sounds to me more like a job for awk. -- Chris Kloiber -- Shrike-list mailing list Shrike-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/shrike-list