From owner-freebsd-questions@FreeBSD.ORG Mon Oct 30 15:42:25 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 3C08816A403 for ; Mon, 30 Oct 2006 15:42:25 +0000 (UTC) (envelope-from jan.grant@bristol.ac.uk) Received: from diri.bris.ac.uk (diri.bris.ac.uk [137.222.10.112]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6981E43D76 for ; Mon, 30 Oct 2006 15:42:15 +0000 (GMT) (envelope-from jan.grant@bristol.ac.uk) Received: from mail.ilrt.bris.ac.uk ([137.222.16.62]) by diri.bris.ac.uk with esmtp (Exim 4.63) (envelope-from ) id 1GeZHA-0002kX-El; Mon, 30 Oct 2006 15:42:15 +0000 Received: from cse-jg.cse.bris.ac.uk ([137.222.12.37]:60782) by mail.ilrt.bris.ac.uk with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA:32) (Exim 4.50) id 1GeZGt-000377-Ku; Mon, 30 Oct 2006 15:41:55 +0000 Date: Mon, 30 Oct 2006 15:41:55 +0000 (GMT) From: Jan Grant X-X-Sender: cmjg@tribble.ilrt.bris.ac.uk To: Ensel Sharon In-Reply-To: Message-ID: <20061030153957.J52313@tribble.ilrt.bris.ac.uk> References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-ILRT-MailScanner: Found to be clean X-ILRT-MailScanner-SpamCheck: not spam, SpamAssassin (score=-1.061, required 6, autolearn=disabled, ALL_TRUSTED -1.44, AWL 0.05, PLING_QUERY 0.33) X-ILRT-MailScanner-From: jan.grant@bristol.ac.uk X-Spam-Status: No X-Spam-Score: -1.1 X-Spam-Level: - Cc: freebsd-questions@freebsd.org 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:42:25 -0000 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 workarounds. Cheers, jan -- jan grant, ISYS, University of Bristol. http://www.bris.ac.uk/ Tel +44 (0)117 3317661 http://ioctl.org/jan/ stty intr ^m