I want to avoid having temporary files laying around if my program crashes.
UNIX is wonderful in that you can keep a file open - even after you delete it.
So if you open the file, immediately delete it, and then do the slow processing, chances are high that even if you program crashes, the user will not have to clean up the file.
In shell I often see something similar to:
generate-the-file -o the-file
[...loads of other stuff that may use stdout or not...]
do_slow_processing < the-file
rm the-file
But if the program crashes before rm
the user will have to clean up the-file
.
In Perl you can do:
open(my $filehandle, "<", "the-file") || die;
unlink("the-file");
while(<$filehandle>) {
# Do slow processing stuff here
print;
}
close $filehandle;
Then the file is removed as soon as it is opened.
Is there a similar construct in shell?