file redirect is destroying the file ? Help!

Jan Grant jan.grant at
Mon Oct 30 15:42:25 UTC 2006

On Mon, 30 Oct 2006, Ensel Sharon wrote:

> I have a script that, among other things, removes a line from
> /etc/ftpchroot.
> I do this with this method:
> cat /etc/ftpchroot | grep -v $remove > /etc/ftpchroot
> Easy.  You cat all of the file except the line you want to remove, and
> redirect it back to itself.
> The problem is, about 50% of the time, I end up with an empty ftpchroot
> file.  It is zero bytes.  This obviously has nothing to do with a bad
> variable, since if it wasn't there, the starting file and ending file
> would just be identical.
> Instead, I get an empty file.  I have reproduced this with other files in
> other places - works some of the time, other times gives me an empty file.
> What gives ?
> (note, I know a lot of ways to work around this - so I'm not so much
> asking how to fix this, as I am asking "why does this happen" ?)

It happens because the two processes either side of the pipe are 
competing for CPU resource. If the "cat" gets to run first then it'll 
slurp in the contents of the existing file (which will probably fit into 
one stdio buffer) and everything's hunky-dory. If the other half gets to 
go first, then the shell will open the file for output, truncating it in 
the process, and exec grep. At that point the "cat" will have another go 
and find nothing there.

Basically, what you've written is buggy and you need to use one of your 


jan grant, ISYS, University of Bristol.
Tel +44 (0)117 3317661
stty intr ^m

More information about the freebsd-questions mailing list