remove temporary file ?

Hi all,

In the script I am creating a temporary file with process id as temp.txt.$$

I want to remove this tomporary file first from the current directory when i'll run the same script next time.
Note: Every time when the script executes then it has unique process id and it'll create a unique temporary file. $$ shows the process id.

How to do that please let me know.

Thnks in advance.

:slight_smile:

Hi,

rm temp.txt.*

This will remove the temporary file.

Regards,
Chella

If the file is truely a temporary file then delete it when your script finishes....

#!/bin/sh

cleanup()
{
    for d in $LAUNDRY_LIST
    do
        if test -f "$d"
        then
            rm "$d"
        fi
    done
}

trap cleanup 0

blah >my.tmp.$$

LAUNDRY_LIST="$LAUNDRY_LIST my.tmp.$$"

dostuff

do more stuff

Thanks buddy...

Can i put this in Conditional statement ?

As i used following :
if [ -f tempdspmq.txt.* ] then
rm tempdspmq.txt.* ;
fi

But its not working...Please let me know, any solution to this.
:slight_smile:

Thnks porter, I have checked your script too. Thnks !!
:slight_smile:

Why not

rm -f "tempdspmq.txt.*" 2> /dev/null

As such, porter's approach is more elegant.

Thanks Vino.

I liked this Optimized approach !! Thanks :smiley: