Pipeline and file integrity?

Hi,

As far as I understand, all operators (commands) in the pipeline start running in parallel.

I wonder if this approach can cause file integrity problems?

$ sed "s/foo/bar/" /some/file | tee /some/file

(I'm aware that sed is capable of inplace editing, but GNU and non-GNU versions differ in this regard. A cross-platform solution is necessary.)

I wonder if race condition can arise, and the file might get overwritten before the reading hits the EOF?

Thanks in advance.
 
It would be better to write the file to a tmp file and move it over the original after the operation is complete. If you need cross platform you can look into POSIX.
 
Thank you, I did exactly that. Found no guarantee that the pipe respects file states in the same operation.

$ sed "..." /some/file > /some/file.tmp
$ mv /some/file.tmp /some/file
 
Back
Top