How to read sequentially from a file in SH (not KSH)

stephen.m.kennedy smk at cbnews.cb.att.com
Mon Apr 29 21:46:06 AEST 1991


>In an article, afc at shibaya.lonestar.org (Augustine Cano) writes:
>
>while read VAR1 VAR2 < $FILENAME; do
>  echo $VAR1 $VAR2
>done
>
>but I keep reading the first line in the file over and over.  Nothing in
>the manual page seems to be usable in this situation.  How is this done
>under SH?  Is it possible? 

How about:

exec 3<$FILENAME
while read VAR1 VAR2 0<&3; do
  echo $VAR1 $VAR2
done
exec 3<&-
# any variable set in while loop will still be set here
# ...


or:

readit()
{
    while read VAR1 VAR2; do
        echo $VAR1 $VAR2
    done
}

readit <$FILENAME

Steve Kennedy
smk at cbosgd.att.com



More information about the Comp.unix.shell mailing list