How to do <cmd> file | hold file

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Sun Sep 9 13:04:53 AEST 1990


In article <1215 at tardis.Tymnet.COM> jms at tardis.Tymnet.COM (Joe Smith) writes:
> In article <2274 at ns-mx.uiowa.edu> jlhaferman at l_cae07.icaen.uiowa.edu (Jeffrey Lawrence Haferman) writes:
    [ how to sort a file and place output back into same file? ]
  [ sort -o file file works ]
> In general, you have to find a suitable location to hold the file, then
> mv it to the destination.

No, you don't.

  % ( rm file; foo > file ) < file

foo's input will point at the old file; its output will go into the new
file. If foo reads all its input early and close()s to indicate this,
the disk space for the original will immediately disappear, provided
that it had only one link. If it had several links then the others will
stay around, entirely unaffected.

(I think cp foo bar should work like ( rm -f bar; cat > bar ) < foo, so
that other links to bar won't be affected; but POSIX 1003.2 apparently
disagrees and has cp foo bar work like cat < foo > bar. Oh, well.)

---Dan



More information about the Comp.unix.shell mailing list