[comp.unix.questions] make temp files?

ldh@hcx1.SSD.HARRIS.COM (09/20/88)

I was wondering if there is some "readymade" way that a shell can create a
unique temporary file, as in the C routine "mktemp(XXXXX)".  Thanks!


Leo Hinds

*net:	ldh@hdw.harris.com	uunet!hcx1!hardy!ldh
usps:	Harris CSD, 2101 W. Cypress Creek Rd, Ft Lauderdale, FL  33309
at&t:   (305)973-5229

predict@charon.unm.edu (Andrew R. Large) (09/21/88)

In article <48200010@hcx1> ldh@hcx1.SSD.HARRIS.COM writes:
>
>I was wondering if there is some "readymade" way that a shell can create a
>unique temporary file, as in the C routine "mktemp(XXXXX)".  Thanks!
>
>Leo Hinds

Both csh and sh replace $$ with the current PID.  Using that, you can
create and use a unique tmp file name with(sh):

	file=/tmp/aa$$
	stuff >> $file
	     ...
	rm $file

-- 

 -=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
   *                          Andrew R. Large                              *
  **  (work) 505/255-8611 ------|     Univ of New Mexico EECE Department   **
 ***  (home) 505/888-4010       |---> Management Sciences, Inc. [MSI]      ***
****                            _Babooshka!_                               ****
 ***  Usenet: {convex,gatech,ucbvax,csu-cs,anl-mcs}!unmvax!charon!predict  ***
  **  Internet: predict@charon.UNM.EDU                                     **
   *          If I am quoted, my employers will deny my existence.         *
 -=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-

guy@gorodish.Sun.COM (Guy Harris) (09/21/88)

> I was wondering if there is some "readymade" way that a shell can create a
> unique temporary file, as in the C routine "mktemp(XXXXX)".  Thanks!

Not exactly like "mktemp", but both the Bourne (and Korn) and C shells have a
variable "$$", which expands to the process ID of the shell.  This is what most
shell scripts use for temporary files, since the traditional UNIX trick for
making unique file names is to stuff the process ID of the creating process
into their names.  "mktemp" does other things, of course, that the shell script
would have to do itself.

BTW, you probably want to use "trap" in the Bourne or Korn shell, and "onintr"
in the C shell, to make sure the shell script cleans up its temporary files if
interrupted.  Not cleaning up your temporary files when interrupted is
considered Very Rude, both for programs and shell scripts.  (Yes, I know,
SunView windows don't clean up; I consider them Very Rude....)

wu@spot.Colorado.EDU (WU SHI-KUEI) (09/21/88)

In article <48200010@hcx1> ldh@hcx1.SSD.HARRIS.COM writes:
>
>I was wondering if there is some "readymade" way that a shell can create a
>unique temporary file, as in the C routine "mktemp(XXXXX)".  Thanks!

Just use $$ somewhere in the file name - $$ expands to the current pid and is
guaranteed to be unique.  For instance

	temp.$$

will do just fine.

Just a guest here.  In real life:
Carl Brandauer
{ncar|stcvax}!nbires!bdaemon!carl

gwyn@smoke.ARPA (Doug Gwyn ) (09/22/88)

In article <48200010@hcx1> ldh@hcx1.SSD.HARRIS.COM writes:
>I was wondering if there is some "readymade" way that a shell can create a
>unique temporary file, as in the C routine "mktemp(XXXXX)".  Thanks!

mktemp() does not create a file, it creates a file NAME.
A similar shell feature would be
	/usr/tmp/prefix$$

dberg@cod.NOSC.MIL (David I. Berg) (09/22/88)

In article <48200010@hcx1>, ldh@hcx1.SSD.HARRIS.COM writes:
> 
> I was wondering if there is some "readymade" way that a shell can create a
> unique temporary file, as in the C routine "mktemp(XXXXX)".  Thanks!
> 
The symbolic variable $$ will take on the value of the process_id 
executing the shell.  I use this all the time to create uniquely named
temporary files; eg. /usr/tmp/$$.

-- 
David I. Berg (dberg@nosc.mil)
GENISYS Information Systems, Inc., 4250 Pacific Hwy #118, San Diego, CA 92110
MILNET: dberg@nosc.mil
UUCP:   {ihnp4 akgua decvax dcdwest ucbvax}!sdcsvax!noscvax!dberg

twb@hoqax.UUCP (T.W. Beattie) (09/24/88)

In article <48200010@hcx1>, ldh@hcx1.SSD.HARRIS.COM writes:
> 
> I was wondering if there is some "readymade" way that a shell can create a
> unique temporary file, as in the C routine "mktemp(XXXXX)".  Thanks!
> Leo Hinds

The shell variable "$" contains the current process number.
For example:
	>TEMP$$
would create a file named
	TEMP1234
if the current process was number 1234.

DISCLAIMER: I suppose there are shells which don't provide this.
Tom.