bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: errexit does not exit script in subshells


From: Linda Walsh
Subject: Re: errexit does not exit script in subshells
Date: Fri, 01 Feb 2008 11:56:16 -0800
User-agent: Thunderbird 2.0.0.9 (Windows/20071031)



Michael Potter wrote:
Bash Bunch,

Not surprisingly, bash does not exit the script when an error is detected in
a subshell.

I am hopeful that someone has a solution to this (other than: be careful to
not use subshells).
----
This seems to work unless I'm missing something...

#!/bin/bash

set -o errexit -o noclobber -o nounset -o pipefail

function traperr
{
   echo "TRAPERROR: ${BASH_SOURCE[0]} ${LINENO}"
   exit 1   # does not seem to affect the program.
   # return 1 # does not seem to affect the program.
}

set -o errtrace
trap traperr ERR

echo -n "Test one: "
pipesave=$(set +o |grep pipefail)
set +o pipefail
cat /etc/passwd |while read aLine; do
        # note: ending following 'test=' line with a "|true" will
        # stop failure from occurring or being detected, if this is
        # what you want..., if not, then set -o pipefile and it
        # will 'fail' if any are false.
        test=$(true | false | true |false)
done
test "$?" != 0 && exit 47
$pipesave       # set pipesave back to original value

echo -n "ONE Ok; Test two: "

while read aLine; do
   test=$(true | false | true |false);
done </etc/passwd

echo "TWO"

test=$(true | false | true |false);

echo "END"

exit 0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]