Unlinked temp files in sh scripts

Leo de Wit leo at philmds.UUCP
Thu Apr 27 03:06:00 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.$$

For C programs this is already a common technique when dealing with
temp files:  open a file (giving you a descriptor), then unlink it. As
long as the file is not closed, it still can be accessed via the
descriptor. Perhaps it is not so commonly used in sh scripts.

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

This doesn't work, since 5<&4 effectively is dup2(4,5). And although
this gives you a new descriptor, it will have only one seek pointer in
the file.  This means if you read to EOF on 4, you're also on EOF on
5. Example:

----------- Start of sample program -----------
#! /bin/sh
exec 3>temp.$$ 4<temp.$$
exec 5<&4
rm -f temp.$$

cat >&3 <<EOT
first
second
third
EOT

echo From 4:
cat <&4

# rewind 5

echo From 5:
cat <&5
----------- End of sample program -------------
----------- Output of sample program ----------
>From 4:
first
second
third
>From 5:
----------- End of output of sample program ---

The shell has no means to rewind that I know of; however, a simple
one-liner will do the trick (add error checking of your fancy):

----- rewind.c:
main(argc,argv) int argc; char **argv; { lseek(atoi(argv[1]),0L,0); }

After uncommenting the '# rewind 5' the output from 5 is the same as
that from 4!

	 Leo.

P.S. The descriptor '5' is not needed after all; all one has to do is
rewind 4 instead of rewind 5, and then reuse descriptor 4.



More information about the Comp.unix.wizards mailing list