Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!munnari!otc!uqcspe!tony From: tony@uqcspe.OZ (Tony O'Hagan) Newsgroups: comp.unix.wizards Subject: sh(1) traps & exit status Message-ID: <1639@uqcspe.OZ> Date: Thu, 29-Oct-87 21:07:23 EST Article-I.D.: uqcspe.1639 Posted: Thu Oct 29 21:07:23 1987 Date-Received: Thu, 5-Nov-87 05:58:02 EST Reply-To: tony@uqcspe.oz (Tony O'Hagan) Organization: Computer Science, Queensland Uni, Australia Lines: 34 Keywords: shell, trap, status In the process of designing systems written (or prototyped) in bourne shell, I often have scripts that are required to return a useful exit status. The problem arises when using traps to clean up and reliably return the correct status when I'm finnished. My current solution looks something like this :- #!/bin/sh : : ts=2 # default trap exit status tdir=/tmp/myprog.$$ trap 'rm -fr $tdir; exit $ts' 0 1 2 3 15 mkdir $tdir || exit 2 input=$tdir/input output=$tdir/output : exit 2 # some sort of error : ts=1; exit 1 # return exit status 1 : ts=0; exit 0 # good-bye I hate having to assign the exit status to a variable before every exit. This doesn't work: trap 'ts=$?; rm -fr $tdir; exit $ts' 0 1 2 3 15 Anyone found a simpler, cleaner method for this problem ? Tony O'Hagan Computer Science Department, ACSnet: tony@uqcspe.oz University of Queensland, UUCP: ..!uunet!munnari!uqcspe.oz!tony St Lucia, Brisbane, 4067 JANET: uqcspe.oz!tony@ukc Australia. Internet: tony%uqcspe.oz@uunet.uu.net Phone: +61 7 377 4125 Smar