From owner-freebsd-hackers Sat Aug 28 5:29:22 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from dorifer.heim3.tu-clausthal.de (dorifer.heim3.tu-clausthal.de [139.174.243.252]) by hub.freebsd.org (Postfix) with ESMTP id 3BE8F14DB9 for ; Sat, 28 Aug 1999 05:29:16 -0700 (PDT) (envelope-from olli@dorifer.heim3.tu-clausthal.de) Received: (from olli@localhost) by dorifer.heim3.tu-clausthal.de (8.8.8/8.8.8) id OAA28773 for freebsd-hackers@FreeBSD.ORG; Sat, 28 Aug 1999 14:25:51 +0200 (CEST) (envelope-from olli) Date: Sat, 28 Aug 1999 14:25:51 +0200 (CEST) From: Oliver Fromme Message-Id: <199908281225.OAA28773@dorifer.heim3.tu-clausthal.de> To: freebsd-hackers@FreeBSD.ORG Subject: Re: Help with exit status in shell script Organization: Administration Heim 3 Reply-To: freebsd-hackers@FreeBSD.ORG MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Newsreader: TIN [version 1.2 RZTUC(3) PL2] Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Roger Hardiman wrote in list.freebsd-hackers: > There is a bug in the PicoBSD build shell script in and I have no idea > how to fix it. As a result, build errors are not caught. > It is all to do with Exit Status of programs called from a shell script. > Please help. > > The code fragment from /usr/src/release/picobsd/build/build is > ./stage1 2>&1 | tee stage1.out > if [ "X$?" != "X0" ] ; then > echo "^G" > echo "-> ERROR in \"${i}\" script. Aborting the build process." > exit 10 > fi > > Build calls Stage1. Stage1 will return with an error code in some cases > and we want to trap this and halt the Build script. > > ./stage1 2>&1 | tee stage1.out > if [ "X$?" != "X0" ] ; then > > Normally, $? will return the Exit Status of the last executed program. > However, due to the pipe through Tee, the Exit Status I get is the > exit status of Tee and not the exit status of the Stage1 script. > > I still want to output the stage1 script to screen and a log file. > How can I do this and preserve the exit status for the Build script. There are several solutions, but all of them are somewhat ugly... One approach would be to use a named pipe, run stage1 in the background and then wait for it, grabbing its exit status: FIFO=/tmp/`basename $0`.$$.fifo trap "rm -f $FIFO" 1 2 15 mkfifo $FIFO ./stage1 > $FIFO 2>&1 & STAGE1PID=$! tee < $FIFO stage1.out wait $STAGE1PID if [ $? != 0 ]; then ... rm -f $FIFO Maybe it's possible to open a new shell descriptor (e.g. 3) instead of a named pipe, but I haven't tried this. Another way would be to put the exit code into a temporary file, like this: trap "rm -f stage1.result" 1 2 15 ( ./stage1 2>&1 echo $? > stage1.result ) | tee stage1.out if [ `cat stage1.result` != 0 ]; then ... rm -f stage1.result Regards Oliver -- Oliver Fromme, Leibnizstr. 18/61, 38678 Clausthal, Germany (Info: finger userinfo:olli@dorifer.heim3.tu-clausthal.de) "In jedem Stück Kohle wartet ein Diamant auf seine Geburt" (Terry Pratchett) To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message