bug-bash
[Top][All Lists]
Advanced

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

SOLUTION: errexit does not exit script in subshells


From: Michael Potter
Subject: SOLUTION: errexit does not exit script in subshells
Date: Thu, 31 Jan 2008 17:58:42 -0600

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 <address@hidden> 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 via email to

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