SOLUTION: errexit does not exit script in subshells

17 views
Skip to first unread message

Michael Potter

unread,
Jan 31, 2008, 6:58:42 PM1/31/08
to bug-...@gnu.org
Bash Bunch,

Here is how I 'solved' the problem:

function traperr
{
echo "ERROR: ${BASH_SOURCE[0]} ${LINENO}"

# if BASH_SUBSHELL is 0, then script will exit anyway.
if (( $BASH_SUBSHELL >= 1 ))
then
kill $$
fi
}

I put solved in quotes because I do not really care for this solution.
I hope someone will comment with a graceful solution.

--
Michael Potter

On Jan 31, 2008 11:43 AM, Michael Potter <pot...@gmail.com> 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).
>
> Here is the test run:
> -------------------
> ./subshellnofail.sh
> BEGIN
> ERROR: ./subshellnofail.sh 10 <- would like the script to stop here.
> ONE
> ERROR: ./subshellnofail.sh 10
> -------------------
>
> Here is the sample script:
> ------------------
> #!/bin/bash
>
> set -o errexit
> set -o noclobber
> set -o nounset
> set -o pipefail # if you fail on this line, get a newer version of
> bash.
>
> function traperr
> {
> echo "ERROR: ${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 "BEGIN"
>
> cat /etc/passwd |while read aLine
> do
> # in a subshell
> test=$(true | false | true |false);
> done
>
> echo "ONE"
>
> while read aLine
> do
> # not in a subshell
> test=$(true | false | true |false);
> done </etc/passwd
>
> echo "TWO"
>
> test=$(true | false | true |false);
>
> echo "END"
>
> exit 0
> -----------------
>
> echo $BASH_VERSION
> 3.2.17(1)-release
>
>

Reply all
Reply to author
Forward
0 new messages