======================================================

       −e    When this option is on, when any command fails (for any of the reasons listed in Section 2.8.1, Consequences of Shell Errors or by  return‐
             ing an exit status greater than zero), the shell immediately shall exit with the following exceptions:

              1. The  failure  of any individual command in a multi-command pipeline shall not cause the shell to exit. Only the failure of the pipeline
                 itself shall be considered.

              2. The −e setting shall be ignored when executing the compound list following the while, until, if, or  elif  reserved  word,  a  pipeline
                 beginning with the !  reserved word, or any command of an AND-OR list other than the last.

              3. If  the exit status of a compound command other than a subshell command was the result of a failure while −e was being ignored, then −e
                 shall not apply to this command.

             This requirement applies to the shell environment and each subshell environment separately. For example, in:

                 set -e; (false; echo one) | cat; echo two

             the false command causes the subshell to exit without executing echo one; however, echo two is executed because  the  exit  status  of  the
             pipeline (false; echo one) | cat is zero.

==========================


#!/bin/bash

set -e

function myf {
    echo "Enter myf"
    false
    echo "Exit myf"
}

echo "Start"
myf
echo "End"



$ ./x.sh
Start
Enter myf


=======================

#!/bin/bash

set -e

function myf {
    echo "Enter myf"
    false
    echo "Exit myf"
}

echo "Start"
myf || true
echo "End"


$ ./x.sh
Start
Enter myf
Exit myf
End


========================

#!/bin/bash

set -e

function myf {
    echo 'myf' >&2
    exit 2
}

function test {
    a=$(myf)
    echo "TEST DONE"
}

a=$(test)
echo "DONE"

$ ./x.sh
myf
DONE


===========================

#!/bin/bash

set -e

function myf {
    echo 'myf' >&2
    exit 2
}

a=$(myf)
echo "TEST DONE"



$ ./x.sh
myf


====================

#!/bin/bash

set -e

function myf {
    echo 'myf' >&2
    false
    echo "myf DONE"
}

a=$(myf)
echo "DONE"



$ ./x.sh
myf
DONE


=======================

#!/bin/bash

set -e

function myf {
    echo 'myf' >&2
    false
    echo "myf DONE"
}

function test {
    a=$(myf)
    echo 'test DONE'
}

a=$(test)
echo "DONE"


$ ./x.sh
myf
DONE


'Language > Bash' 카테고리의 다른 글

Bash print call stack at ERR(trap)  (0) 2017.03.08


At head of script file:

(CODE-1)
trap 'on_error "${FUNCNAME[@]} ${BASH_LINENO[@]} $LINENO"' ERR
trap 'on_exit' EXIT

Someone may wonder why don't write code like below because it is easier to handle arguments.

(CODE-2)
trap 'on_error "$LINENO ${FUNCNAME[@]} ${BASH_LINENO[@]}"' ERR
trap 'on_exit' EXIT

I don't know there is any documented information related with this. But, results of my experiments are saying that (CODE-2) doens't work as expected.
In case of (CODE-2), my test shows that only latest function-stack information is passed to 'on_error' trap function.
I don't have any idea about the reason. More investigation is required for this.
But anyway, (CODE-1) works well.
So, you can use those arguments to print function call stack at bash.
You may need to use 'BASH_SOURCE' array too, if your bash uses other files, too.



And there is one interesting case. See follow code.

<< Test environment >>
bash: GNU bash, version 4.3.46(1)-release (x86_64-pc-linux-gnu)
OS: Linux XXXX 4.4.0-64-generic #85-Ubuntu SMP Mon Feb 20 11:50:30 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

-------------------------- TEST-1 -----------------------------
<< a.sh >>
function on_err() {
    echo "error"
}

function on_exit() {
    echo "exit"
}

trap on_err ERR
trap on_exit EXIT

myval=$(echo u | grep p)  # global variable


$ bash -eE a.sh
error
exit

-------------------------- TEST-2 -----------------------------

<< a.sh >>
function on_err() {
    echo "error"
}

function on_exit() {
    echo "exit"
}

trap on_err ERR
trap on_exit EXIT

function f0() {
    local myval=$(echo u | grep p)  # local variable in function
}

f0

$ bash -eE a.sh
exit

---------------------------------------------------------------

Even if errtrace is enabled(-E option), 'error' is NOT printed at TEST-2.
Then, is this means 'ERR' is NOT trapped at TEST-2? That is, does 'on_err' not executed?
Let's have a look following code.

-------------------------- TEST-3 -----------------------------

<< a.sh >>
function on_err() {
    echo "error"
}

function on_exit() {
    echo "exit"
}

trap on_err ERR
trap on_exit EXIT

function f0() {
    myval=$(echo u | grep p)  # NOT local variable anymore.
}

f0

$ bash -eE a.sh
error
exit

-------------------------- TEST-4 -----------------------------

<< a.sh >>
function on_err() {
    echo "error" 1>&2  # echo to standard error.
}

function on_exit() {
    echo "exit"
}

trap on_err ERR
trap on_exit EXIT

function f0() {
    local myval=$(echo u | grep p)  # local variable
}

f0

$ bash -eE a.sh
error
exit

-------------------------- TEST-5 -----------------------------


function on_err() {
    echo "error" 1>&2  # echo to stderr
}

function on_exit() {
    echo "exit"
}

trap on_err ERR
trap on_exit EXIT

myval=$(echo u | grep p)  # assign to global variable.


$ bash -eE a.sh
error
error
exit


-------------------------- TEST-6 -----------------------------


function on_err() {
    true  # There is no echo
}

function on_exit() {
    echo "exit"
}

trap on_err ERR
trap on_exit EXIT

myval=$(echo u | grep p)  # asign to global variable


$ bash -eE a.sh
exit

---------------------------------------------------------------

It's very interesting, isn't it?
Further investigation will be continued for this issue.



'Language > Bash' 카테고리의 다른 글

[Bash] 'set -e' inheritance...???  (0) 2017.05.19

+ Recent posts