Bourne Shell FOR loop confusion

James_Rogers jim at hp-ptp.HP.COM
Thu Aug 17 03:40:59 AEST 1989


/ hp-ptp:comp.unix.questions / kevin at msa3b.UUCP (Kevin P. Kleinfelter) /  6:16 am  Aug  1, 1989 /
>What I really want to do is to redirect input to "read", but read is
>non-redirectable (according to my doc, and experience on my system).
>I was going to attemtpt this as follows
>
>   for i in 1
>   do
>      read a b c
>   done < /tmp/foo
>
>If I "echo a, b, c" inside the loop, I get the expected values from the file.
>If I "echo a, b, c" after the loop, I see that they have reverted to their
>values prior to the loop.

You could try the following form of redirection:

for nextfile in a b c
do
	cat $nextfile | {
		while [ -r $nextfile ]
		do
			read aline
			if [ $? -ne 0 ]
			then
				break
			fi
			echo $aline
		done
	}
done


I know that this is not pretty, but it does allow you to read through
files a, b, and c from the Bourne shell.



Jim Rogers  at Hewlett Packard Industrial Applications Center
	       Sunnyvale, California



More information about the Comp.unix.questions mailing list