jeudi 5 mars 2015

Bash Shell Script Array Length Off By One


The length of an array in my bash shell script appears to be off by one. I have 11 elements in a text file that I am reading in to an array, but my array length seems to be 12.



(( count = 0 ))
while read students[$count] ; do
(( count++ ))
done < students.dat

echo $count

ArrayLength=$((${#students[@]}))

echo $ArrayLength


This code outputs:



11
12


The 11 makes sense since the count increment occurs after the read occurs and starts with 0--indicating 11 elements read in.


But the 12 is mysterious.


Here is the data file:



Ann
Bob
Cindy
Dean
Emily
Frank
Ginger
Hal
Ivy
Justin
Karen


(names appear on their own lines, but I couldn't format it that way in this post)


I've double checked with multiple utilities and there is NOT a blank line at the end of the file, or any trailing spaces on any line.



Aucun commentaire:

Enregistrer un commentaire