"Taking a LiveFire Labs' course is an excellent way to learn
Linux/Unix. The lessons are well thought out, the material is
explained thoroughly, and you get to perform exercises on a real
Linux/Unix box. It was money well spent."
Ray S.
Pembrook Pines, Florida
LiveFire Labs' UNIX and Linux Operating System Fundamentals
course was very enjoyable. Although I regularly used UNIX systems
for 16 years, I haven't done so since 2000. This course was a
great refresher. The exercises were fun and helped me gain a real
feel for working with UNIX/Linux OS. Thanks very much!"
Ming Sabourin
Senior Technical Writer
Nuance Communications, Inc.
Montréal, Canada
Read more student testimonials...
Receive UNIX Tips, Tricks, and Shell Scripts by Email
LiveFire Labs' UNIX Tip,
Trick, or Shell Script of the Week
Using $$ to Create Unique Filenames in UNIX or Linux
There may be times when your Korn shell script will need to
dynamically create temporary files that may be accessed intermittently
while the script is running. How would you ensure the integrity of
these files if two or more users were running the script
simultaneously? One option is to use the $ shell parameter to create
a unique filename for each temporary file.
The $ shell parameter is a special parameter (set by the Korn shell*)
that contains the process ID, or the PID, of the running shell. The
PID is a unique integer that is assigned to each process when it is
created. The system and process' owner use this number to manage the
process.
To access the value stored in $, you simply preface it with a second
$. For example, the following command will print the running shell's
PID to standard output:
# print $$
31853
To uniquely name temporary shell script files, you would append the
value stored in $ to the end of the temporary file's base name:
/tmp/filename.$$
This syntax may be used when you use exec to open a file for writing:
exec 3> /tmp/filename.$$
...or within simple I/O redirection statements such as:
print "Script starting at $(date +%m%d%Y_%H%M%S)" >
/tmp/filename.$$
Like anything else in UNIX, there are multiple methods for generating
unique filenames but using a shell's PID is one of the most
frequently-used techniques.
*You cannot assign your own value to this parameter