home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 16.15 Script with a :-) for UNIX Converts: dir, ..., ... Chapter 16
Where Did I Put That?
Next: 16.17 Getting Directory Name from a File's Pathname
 

16.16 Picking a Unique Filename Automatically

Shell scripts, aliases, and other programs can need temporary files to hold data used later. If the program will be run more than once, or if the temp file needs to stay around after the program is done, you need some way to make a unique filename.

One way is with the shell's process ID number (38.3 ) , available in the $$ parameter and often used in the /tmp directory . (21.3 ) You might name a file /tmp/MYPROG$$ ; the shell will turn that into something like /tmp/MYPROG1234 or /tmp/MYPROG28471 . If your program needs more than one temporary file, add an extra unique character to the names:

errs=/tmp/MYPROGe$$ output=/tmp/MYPROGo$$

Remember the 14-character filename limit on some older UNIXes. $$ usually makes two to five characters.

If your UNIX doesn't have a date command that takes a + parameter to change its output format, you should get one (51.10 ) . For example, to output the m onth, d ay, H our, M inute, and S econd:

% date

 Thu May 30 07:21:13 EDT 1991
% date +'%m%d%H%M%S'

 0530072124

Use a + parameter and backquotes (`` ) (9.16 ) to get a temp file named for the current date and/or time. For instance, on May 31 the command below would store foo.0531 in the Bourne shell variable temp . On December 7, it would store foo.1207 :

 temp=foo.`date +'%m%d'`

Article 21.3 shows another system for temporary files.

- JP


Previous: 16.15 Script with a :-) for UNIX Converts: dir, ..., ... UNIX Power Tools Next: 16.17 Getting Directory Name from a File's Pathname
16.15 Script with a :-) for UNIX Converts: dir, ..., ... Book Index 16.17 Getting Directory Name from a File's Pathname

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System