Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!munnari!uqcspe!tony From: tony@uqcspe.OZ (Tony O'Hagan) Newsgroups: comp.unix.wizards Subject: Re: sh(1) traps & exit status Message-ID: <1652@uqcspe.OZ> Date: Wed, 4-Nov-87 22:14:00 EST Article-I.D.: uqcspe.1652 Posted: Wed Nov 4 22:14:00 1987 Date-Received: Sun, 8-Nov-87 01:48:30 EST References: <1639@uqcspe.OZ> <20@grand.sm.unisys.com> Reply-To: tony@uqcspe.oz (Tony O'Hagan) Organization: Computer Science, Queensland Uni, Australia Lines: 37 In article <20@grand.sm.unisys.com> you write: >In article <1639@uqcspe.OZ> tony@uqcspe.oz (Tony O'Hagan) writes: >|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 > >You are springing the trap twice. Use this instead: > > trap exit 1 2 3 15 > trap 'ts=$?; rm -fr $tdir; exit $ts' 0 Thanks for you thoughts ... I realised that interupts cause the trap to be done twice, I guess I'm just lazy when it's only doing "rm -f ..." . Although when it does matter, your solution is cleaner than turning off traps within the trap !! What I'm really concerned about is that "exit 2" does not set $? for the trap. I often have something like :- trap 'rm -fr $tdir; exit $ts' 0 1 2 3 15 [ -s "$1" ] || { echo "$1 is empty" >&2; ts=2; exit 2; } One solution mailed to me by Robert Elz [kre@monet.berkeley.edu] was trap 'ts=$?; rm -fr $tdir; exit $ts' 0 1 2 3 15 [ -s "$1" ] || { echo "$1 is empty" >&2; (exit 2); exit; } which could also use the exit status of "mkdir" in something like :- mkdir $2 || exit I'd appeciate any further ideas, 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 Smart: tony@uqcspe.oz.au