file redirect is destroying the file ? Help!

DAve dave.list at
Mon Oct 30 15:51:09 UTC 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" ?)

I can't answer your question except to say that I have always redirected 
to a tmp file, and then replaced the original after safely closing it 
and testing the tmp file for whatever results I expect.

Just dumping output to overwrite the file I am currently reading was 
always bad juju in my book.


Three years now I've asked Google why they don't have a
logo change for Memorial Day. Why do they choose to do logos
for other non-international holidays, but nothing for

Maybe they forgot who made that choice possible.

More information about the freebsd-questions mailing list