Help with strings in Bourne shell

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


In article <870 at marvin.Solbourne.COM> dce at Solbourne.com (David Elliott) writes:
# In article <1493 at vsedev.VSE.COM> logan at vsedev.VSE.COM (James Logan III) writes:
# >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;

This construct has a BIG problem.  I corrected it in a previous
posting.  

# 
# A typical trick is
# 
# 	exec 3<&0 0<"$INPUTFILE"
# 	while read DEFINITION
# 	do
# 		echo "$DEFINITION"
# 	done
# 	exec 0<&3
# 
# The first exec makes fd 3 a duplicate of fd 0 (stdin), and
# redirects stdin.  The second exec changes fd 0 back to what
# it was.

This has the side effect of redirecting stdin for every command
invoked inside the while loop.  (Take a look at my corrected
posting.)  Besides, it has the same effect as the simpler
construct: 

	while read DEFINITION; do
		echo "$DEFINITION";
	done <$INPUTFILE;


Under System V (on 3B2, Altos, and XENIX at least) the variable
"DEFINITION" is not set in a sub-shell.  To do this you would
have to do something silly like:  

	while `read DEFINITION`; do
		echo "$DEFINITION";
	done;

Perhaps this isn't the case in BSD UNIX.

			-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