Once a process has
opened a file (45.20),
UNIX won't delete the file
until the process closes it.
(The rm command only removes a link to the file from a directory,
not the file itself.)
about whether removing a file while it's open is a good
If you want to run a set of commands from a file, but not let anyone
else read the list of commands you're using, you can write a shell
script that removes itself before doing anything else.
(You should be aware that if you use a filesystem mounted by
NFS will just rename the "removed" file to a
hidden filename (16.11)
Here's a simple self-removing shell script:
% cat doit
rm doit # by now, shell has opened this file; we can remove it
% sh doit
ls: doit not found
cc -target sun4 -c routine.c
Here's a more typical script that opens and removes a file in
% cat delme
temp=/tmp/delme$$ # file in /tmp (could be anywhere)
echo "This is line1.
This is line2.
This is line3." > $temp # put three lines in $temp
ls -l $temp; wc $temp # ls and count lines in the file
exec < $temp # take standard input from $temp
read line; echo $line # read and echo line 1 from $temp
rm $temp; echo rm returned $? # remove $temp link; show status
ls -l $temp; wc $temp # the file is gone...?
read line; echo $line # but file is still open!
read line; echo $line
exec <&- # close standard input (and file)
-rw-rw-r-- 1 jerry 45 Sep 16 12:31 /tmp/delme22743
3 9 45 /tmp/delme22743
This is line1.
rm returned 0
ls: /tmp/delme22743: No such file or directory
wc: cannot open /tmp/delme22743
This is line2.
This is line3.