Unlinked temp files in sh scripts

David Elliott dce at Solbourne.COM
Wed Apr 26 00:26:33 AEST 1989


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.$$

At this point, you can put all the data you want in the temp
file by redirecting to fd 3, as in

	generate_data 1>&3

After this has been done, fd 4 is still a read descriptor
pointing at the beginning of the temp file, so you can
read the data in the file by redirecting from fd 4, or,
if you need to read it multiple time, by dup'ing fd 4
("exec 5<&4", for example, to copy it to fd 5).

When you're finished, there's no need to clean up, since
the file was removed before it was even used.  Of course,
it is possible to kill the program between the time the
temp file is created and the time when it is removed, but
at worst you'll end up with an empty file.

-- 
David Elliott		dce at Solbourne.COM
			...!{boulder,nbires,sun}!stan!dce



More information about the Comp.unix.wizards mailing list