shell file descriptor programming (was: Unlinked temp files)

Kenneth Almquist ka at june.cs.washington.edu
Wed May 3 11:42:42 AEST 1989


stever at tree.UUCP (Steve Rudek) writes:
> Neither works under the ksh (Microport System V/AT 2.4) though both work
> under the bourne shell (tested with the shell script).  The ksh failure is
> absolutely silent.
>
> Obviously the ksh isn't 100% compatible (I've also noticed that function
> recursion which works under sh fails under ksh--but at least it has the
> decency to complain).  Is this sort of file rewind impossible under ksh?
> Any guesses as to why the ksh falls down?  Other significant upward
> incompatabilities?

It's supposed to be a feature.  The idea is that if you save file
descriptors by moving them, you don't necessarily want to pass them
to the programs you run.  Let me explain that last sentence with an
example:

	if test $flag; then exec 3<&0 <file; fi
	program
	if test $flag; then exec <&3 3<&- ; fi

This uses file descriptor 3 to hold the original value of file de-
scriptor zero if the flag is set.  Under the Bourne shell, "program"
will be invoked with file descriptor 3 will be open, and (in principle)
the program could run out of file descriptors because file descriptor 3
is unavailable.  The Korn shell avoids this problem by closing file
descriptor 3 before exec-ing the program.

This Korn shell feature is being considered for inclusion in P1003.2.
The rewind/lseek program is the first example I have seen that is
broken by the feature.  It is possible to work around it by saying

	lseek 0 0 0 <&3

because the feature only applies to file descriptors greater than two.
				Kenneth Almquist



More information about the Comp.unix.wizards mailing list