From owner-freebsd-questions@FreeBSD.ORG Mon Jul 21 05:26:02 2003 Return-Path: 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 D3FEB37B401 for ; Mon, 21 Jul 2003 05:26:02 -0700 (PDT) Received: from conn.mc.mpls.visi.com (conn.mc.mpls.visi.com [208.42.156.2]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3EA1543FCB for ; Mon, 21 Jul 2003 05:26:02 -0700 (PDT) (envelope-from hawkeyd@visi.com) Received: from sheol.localdomain (hawkeyd-fw.dsl.visi.com [208.42.101.193]) by conn.mc.mpls.visi.com (Postfix) with ESMTP id A898181AA for ; Mon, 21 Jul 2003 07:26:00 -0500 (CDT) Received: (from hawkeyd@localhost) by sheol.localdomain (8.11.6p2/8.11.6) id h6LCPvN06304 for freebsd-questions@freebsd.org; Mon, 21 Jul 2003 07:25:57 -0500 (CDT) (envelope-from hawkeyd) X-Spam-Policy: http://www.visi.com/~hawkeyd/index.html#mail Date: Mon, 21 Jul 2003 07:25:57 -0500 From: D J Hawkey Jr To: questions at FreeBSD Message-ID: <20030721122557.GA6212@sheol.localdomain> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.1i Subject: Stupid shell tricks X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: hawkeyd@visi.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Jul 2003 12:26:03 -0000 Hello all. I'll cut right to it: I'm looking for a technique to do "atomic writes" in a shell (sh(1)) script, playing with the trap builtin. It goes something like this: 1: #!/bin/sh 2: 3: trapper () 4: { 5: rm -f $TMPFILE 6: exit 7: } 8: 9: trap trapper HUP PIPE INT QUIT SEGV 10: 11: ... 12: 13: while read LINE; do 14: trap "" HUP PIPE INT QUIT SEGV 15: binary_that_writes $LINE 16: ... 17: done <$TMPFILE 18: 19: trapper Line 9 sets up the general-case signal handler to have the script clean up after itself. Line 11 is stuff where I want the signal handler to do it's thing. Line 14 sets up the "atomic write" by ignoring the signals. So far, so good. Line 16 is my problem; this is where I want something so the script exits _without_ cleaning up when the signal that would have been seen/caught at line 15 occurs. The trap at line 14 is inside the loop 'cuz I figure line 16 will be another trap statement, but I seem to be wrong about this. So far, if the trap at line 14 is anything else, line 15 gets interrupted. Using nohup(1) at line 15 (without the trap at line 14) doesn't trap enough signals. I've tried quite a few different signal things, but none have produced what I'm after. Anyone been here and done that? Oh, yeah, it has to be a portable solution, too. Thanks, Dave -- ______________________ ______________________ \__________________ \ D. J. HAWKEY JR. / __________________/ \________________/\ hawkeyd@visi.com /\________________/ http://www.visi.com/~hawkeyd/