To use temporary files or not to use temporary files?

To use temporary files or not to use temporary files?

Post by Michael Wa » Sat, 01 Aug 1998 04:00:00



This is a repost with Subject line changed:

It is very sad to see that Brian Kernighan and Rob Pike uses temporary
files in the example. I think the temporary can be easily and better
avoided in this case. The modified version is provided at the end. If you
believe the modified version is not better, I would like to hear it.
I make a lot of effort not to use temporary files in my shell scripts,
I do not know if I am alone in this.

And I do not see the value of saving the the original file in the
script:

cp $file $old    # save original file

In "The UNIX Programming Environment", Brian Kernighan and Rob Pike provide

>a brilliant script called 'overwrite':

># overwrite:  copy standard input to output after EOF
># final version

>opath=$PATH
>PATH=/bin:/usr/bin

>case $# in
>0|1)    echo 'Usage: overwrite file cmd [args]' 1>&2; exit 2
>esac

>file=$1; shift
>new=/tmp/overwr1.$$; old=/tmp/overwr2.$$
>trap 'rm -f $new $old; exit 1' 1 2 15    # clean up files


>then
>        cp $file $old    # save original file
>        trap '' 1 2 15    # we are committed; ignore signals
>        cp $new $file
>else
>        echo "overwrite: $1 failed, $file unchanged" 1>&2
>        exit 1
>fi
>rm -f $new $old

#!/bin/ksh
# overwrite:  copy standard input to output after EOF
# modified version

opath=$PATH
PATH=/bin:/usr/bin

case $# in
0|1)    echo 'Usage: overwrite file cmd [args]' 1>&2; exit 2
esac

file=$1; shift


then
        trap '' 1 2 15   # we are committed; ignore signals
        print -r -- "$new" >  $file
else
        echo "overwrite: $1 failed, $file unchanged" 1>&2
        exit 1
fi

 
 
 

1. Adding text to the top of a file without using temporary files

I want to send a text file via "mail". Before I pass the file to mail, I want
to insert at the top of the file a subject line of "Subject: < report name>".

I would dearly like to do this without using temporary files, etc. but I can't
find a way of doing it. I've tried echoing text through pipes, cating the
mail file various ways to try and prefix it with the subject line. I've looked
at sed but, not being a sed expert, I haven't found a solution that way either.

Before anyone asks, there isn't a switch to pass the subject to the mail
command. 8-(

Does anyone know of a one line solution to do this without the creation of
temporary files?

Thanks
Andrew Scott.

=====

Development Centre, IS.        
AT&T Global Information Solutions,
Kingsway West, Dundee, Scotland  DD2 3XX                
Tel: +44 382 592531  Fax: +44 382 622198

2. Samba access by internal LAN ?

3. Temporary files used in shell scripts

4. tcpdump problems with FDDI on Solaris 2.4

5. Temporary menuconfig file not removed by make mrproper

6. Solstice Backup 5.1

7. temporary redirection under a Bourne-shell using "exec"

8. Apache stopped logging!!

9. secure temporary file creation/usage

10. Temporary files?

11. Linux ramdisk for temporary files

12. Temporary Password File

13. qpopper: "Can't open temporary file, do you own it?"