为什么bash不会在测试语法错误时退出?

问题描述 投票:0回答:1

我希望脚本在测试中出现语法错误时退出,但是没有运气:

bash -n "$0" || exit  # check script syntax

#set -xv
set -o nounset
set -o errexit
set -o pipefail # make pipes fail if one of the piped commands fails

if (( != 0 )); then  # syntax error here
  echo "after if"
fi

echo "should not reach this point, but indeed does"

输出为:

./testscript: line 8: ((: != 0 : syntax error: operand expected (error token is "!= 0 ")
should not reach this point, but indeed does

任何解决方案?谢谢

bash testing syntax exit
1个回答
0
投票

使用set -e

bash -n "$0" || exit  # check script syntax
set -e
#set -xv
set -o nounset
set -o errexit
set -o pipefail # make pipes fail if one of the piped commands fails

if (( != 0 )); then  # syntax error here
  echo "after if"
fi

echo "should not reach this point, but indeed does"
© www.soinside.com 2019 - 2024. All rights reserved.