From owner-freebsd-questions@FreeBSD.ORG Mon Oct 30 15:51:09 2006 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id D443016A415 for ; Mon, 30 Oct 2006 15:51:09 +0000 (UTC) (envelope-from dave.list@pixelhammer.com) Received: from ecluster5.tls.net (ecluster5.tls.net [65.196.224.135]) by mx1.FreeBSD.org (Postfix) with SMTP id 5DF2243D49 for ; Mon, 30 Oct 2006 15:51:09 +0000 (GMT) (envelope-from dave.list@pixelhammer.com) Received: (qmail 70203 invoked by uid 89); 30 Oct 2006 15:48:47 -0000 Received: from 64-184-9-181.bb.hrtc.net (HELO ?192.168.0.103?) (ldg%tls.net@64.184.9.181) by auth-ecluster5.tls.net with SMTP; 30 Oct 2006 15:48:47 -0000 Message-ID: <45461F5B.80700@pixelhammer.com> Date: Mon, 30 Oct 2006 10:50:51 -0500 From: DAve User-Agent: Thunderbird 1.5.0.4 (Windows/20060516) MIME-Version: 1.0 To: freebsd-questions@freebsd.org References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: file redirect is destroying the file ? Help! X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 30 Oct 2006 15:51:09 -0000 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. DAve -- 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 Veterans? Maybe they forgot who made that choice possible.