Unlinked temp files in sh scripts

Geoff Clare gwc at root.co.uk
Tue May 2 23:50:44 AEST 1989


In article <871 at marvin.Solbourne.COM> dce at Solbourne.com (David Elliott) writes:
>
>This is a trick that Dave Hitz (hitz at auspex.com) and I worked
>out last year:
>
>	exec 3>temp.$$ 4<temp.$$
>	rm -f temp.$$

A neat idea, but unfortunately it suffers from one big problem - those
hard-coded file descriptor numbers.

Suppose I execute a shell script which uses this trick (although I am
unaware of it) and I use file descriptor 3 on the command line for
some reason.  The result could be disastrous.

The idea could be made to work by having the script execute a program
which returns two free file descriptor numbers.  But is it worth the
bother, just to avoid a "trap" statement?

A neater solution could be had if there was a predefined shell
variable (like $$, $#, etc.) giving the next available file
descriptor.  Maybe the Korn shell has such a thing?
-- 

Geoff Clare    UniSoft Limited, Saunderson House, Hayne Street, London EC1A 9HH
gwc at root.co.uk   ...!mcvax!ukc!root44!gwc   +44-1-315-6600  FAX: +44-1-315-6622



More information about the Comp.unix.wizards mailing list