Help with strings in Bourne shell

James Logan III logan at vsedev.VSE.COM
Fri Apr 28 12:37:50 AEST 1989


In article <1493 at vsedev.VSE.COM> I wrote:
# BTW, you can also read from a specific file by redirecting the
# input to the read command like this:
# 
# 	INPUTFILE="some_file";
# 
# 	while read DEFINITION <$INPUTFILE; do
# 		echo "$DEFINITION";
# 		.
# 		.
# 		.
# 	done;

Oooops, I screwed up!  What I wrote above will read the first line
again and again!  The best solution is:

 	INPUTFILE="some_file";
 
	exec 4<$INPUTFILE;
 	while read DEFINITION 0<&4; do
 		echo "$DEFINITION";
 		.
 		.
 		.
 	done;
	exec 4<&-;

since the alternative construct:

 	INPUTFILE="some_file";
 
 	while read DEFINITION; do
 		echo "$DEFINITION";
 		.
 		.
 		.
 	done <$INPUTFILE;

has the side-effect of redirecting the input of every command
invoked from within the while loop.   

			-Jim

-- 
Jim Logan                           logan at vsedev.vse.com
VSE Software Development Lab        uucp:  ..!uunet!vsedev!logan
(703) 329-4654                      inet:  logan%vsedev.vse.com at uunet.uu.net



More information about the Comp.unix.questions mailing list