While running a script, I want to create a temporary file in /tmp
directory.
After execution of that script, that will be cleaned by that script.
How to do that in shell script?
shell-scripttmp
While running a script, I want to create a temporary file in /tmp
directory.
After execution of that script, that will be cleaned by that script.
How to do that in shell script?
Best Answer
You can make sure that a file is deleted when the scripts exits (including kills and crashes) by opening a file descriptor to the file and deleting it. The file keeps available (for the script; not really for other processes but
/proc/$PID/fd/$FD
is a work-around) as long as the file descriptor is open. When it gets closed (which the kernel does automatically when the process exits) the filesystem deletes the file.