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

Mark Valentine mark at spider.co.uk
Mon Apr 29 20:53:29 AEST 1991


afc> while read VAR1 VAR2 < $FILENAME; do
afc>   echo $VAR1 $VAR2
afc> done
afc>
afc> but I keep reading the first line in the file over and over.  Nothing in
afc> the manual page seems to be usable in this situation.  How is this done
afc> under SH?  Is it possible?

mike> cat $FILENAME | while read VAR1 VAR2
mike> do echo $VAR1 $VAR2
mike> done

Sometimes this doesn't fit into the logic of a shell script.  In such cases
I find that something like following works nicely.

    exec 3< $FILENAME
    while read VAR1 VAR2 <&3
    do echo $VAR1 $VAR2
    done

This is especially handy if you have a couple of files you're reading
simultaneously in the body of the loop, or the conditional isn't based
on the read itself.

		Mark.
-- 
Mark Valentine, Spider Systems <mark at spider.co.uk>            /\oo/\



More information about the Comp.unix.shell mailing list