Pipes and variable scoping inside korn shell scripts

It seems that per ksh '93 spec (and maybe earlier) doing something like
---
cat filename |while read $col1 $col2; do
 array($i)=$col1
 echo ${#array[@]}
done

echo ${#array[@]}
---
causes, for a 5 line file, output of
1
2
3
4
5
0
seemingly because in that ksh spec the pipes run as separate processes.  However, I really need to keep the contents of the array outside the loop.  What can I do?  Am I even misinterpreting the spec?  As a related question, how do I find out which version of ksh I'm running?  FYI, I have a Linux RedHat 6.0 install.

boromirAsked:
Who is Participating?
 
ecwConnect With a Mentor Commented:
Use posix shell or

---
exec 3<filename
while read col1 col2 <&3 ; do
 array($i)=$col1
 echo ${#array[@]}
done

echo ${#array[@]}
---

ie. use exec to open filename on fd 3, and read from that.  this will work in ksh and sh.  a ksh specific would be -
  while read -u3 col1 col2 ; do

0
 
boromirAuthor Commented:
Thanks!  Exactly on target!
0
All Courses

From novice to tech pro — start learning today.