Subject: bin/3452: 1.2C /bin/sh doesn't open file descriptor 3 at loop "done"
To: None <gnats-bugs@gnats.netbsd.org>
From: None <jpeek@jpeek.com>
List: netbsd-bugs
Date: 04/06/1997 08:22:51
>Number:         3452
>Category:       bin
>Synopsis:       1.2C /bin/sh doesn't open file descriptor 3 at loop "done"
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun Apr  6 07:35:00 1997
>Last-Modified:
>Originator:     Jerry Peek
>Organization:
Jerry Peek, jpeek@jpeek.com, http://www.jpeek.com/~jpeek/
>Release:        NetBSD 1.2C
>Environment:
	
System: NetBSD hrothgar.gw.com 1.2D NetBSD 1.2D (HROTHGAR) #0: Sun Apr 6 01:19:11 EST 1997 kim@beowulf.nyc.office.juno.com:/tmp_mnt/src/NetBSD/cvsroot/src/sys/arch/i386/compile/HROTHGAR i386


>Description:
	I get the following message from a redirected-output loop in an
	sh script (one that starts with #!/bin/sh) named "fixsubj":
	
		fixsubj: 3: Bad file descriptor
	
	The loop, which has worked on every other shell I've used it on
	(and also works on bash and "bash -posix"), ends like this:
	
		done 3> $temperr	# END OF while read msg subjnow

>How-To-Repeat:
	Here's a simpler script that makes the same error:

		#!/bin/sh
		out=/tmp/testprog.out
		for arg in a b c
		do
			echo hi this is $arg 1>&3
		done 3> $out

>Fix:
	Open FD3 before the loop starts, like this:

		#!/bin/sh
		out=/tmp/testprog.out
		exec 3>$out
		for arg in a b c
		do
			echo hi this is $arg 1>&3
		done
>Audit-Trail:
>Unformatted: