Date: Sat, 28 Aug 1999 14:25:51 +0200 (CEST) From: Oliver Fromme <olli@dorifer.heim3.tu-clausthal.de> To: freebsd-hackers@FreeBSD.ORG Subject: Re: Help with exit status in shell script Message-ID: <199908281225.OAA28773@dorifer.heim3.tu-clausthal.de>
next in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199908281225.OAA28773>
