cat, pipes, and filters

Geoff Clare gwc at root.co.uk
Tue Jun 4 23:03:32 AEST 1991


In <10918 at chorus.fr> bp at chorus.fr (Bruno Pillard) writes:

>What about:

>  ( /bin/rm $FILE ; sed  s/"$ENTRY"/"$NEWENTRY"/ > $FILE ) < $FILE

>I understand that this may look harmful at first glance because of the
>/bin/rm of your precious file but it  works perfectly for  me under sh
>and (t)csh.

>Is there any problem using that construction ?

Yes!  If any errors occur you lose your data.

Slightly better would be:

  ( /bin/rm $FILE && sed  s/"$ENTRY"/"$NEWENTRY"/ > $FILE ) < $FILE

which would not truncate the file if the "rm" fails, but this still
loses your data if the disk is full.  It's much safer to:

  sed  s/"$ENTRY"/"$NEWENTRY"/ < $FILE > $TMPFILE && mv $TMPFILE $FILE
-- 
Geoff Clare <gwc at root.co.uk>  (Dumb American mailers: ...!uunet!root.co.uk!gwc)
UniSoft Limited, London, England.   Tel: +44 71 729 3773   Fax: +44 71 729 3273



More information about the Comp.unix.shell mailing list