尽管设置了-o errexit set,但Bash代码在第一个错误时没有退出

时间:2018-03-28 22:43:03

标签: bash

我有这个:

my_test() {

    ltt -y
    echo "$? ........echo $LINENO @ `basename $0` done"
    date
    echo "$? ........echo $LINENO @ `basename $0` done"
}
export -f my_test

这按预期失败:

$ /bin/bash -c 'set -o pipefail -o errexit && my_test | ts "%FT%T"' #1

这不是

$ /bin/bash -c 'set -o pipefail -o errexit && my_test | ts "%FT%T" && date +%s' #2

为什么这样?我希望#2失败

输出:

$ /bin/bash -c 'set -o pipefail -o errexit && my_test | ts "%FT%T"' #1
/bin/bash: ltt: command not found 

$ /bin/bash -c 'set -o pipefail -o errexit && my_test | ts "%FT%T" && date +%s' #2
/bin/bash: ltt: command not found
2018-03-28T22:37:45 127 ........echo 1 @ bash done
2018-03-28T22:37:45 Wed Mar 28 22:37:45 UTC 2018
2018-03-28T22:37:45 0 ........echo 3 @ bash done
1522276665 # this comes from date +%s

0 个答案:

没有答案