Using Temporary Files

As presented earlier in this chapter, many options exist when it comes to filenames. Often, scripts might need to create a temporary file in an unpredictable directory, or in the /tmp directory. The script should be designed so that it will not clobber the script user's existing files and will not inadvertently clobber another user's files in the shared /tmp directory. But how can this be achieved without cumbersome file existence and name-checking code?

If you name your temporary file with the shell variable $$ at the end, you will be creating a filename with your process identification number (PID) tacked on the end. Because the system guarantees that two processes with the same PID will never be in existence at the same ...

Get Korn Shell Programming by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.