启用 errexit 选项后,任何失败的命令都会导致脚本退出。对于某些预期会失败的命令,我想反转此行为。那就是:我只想在指示的命令失败时继续。
我目前正在做这样的事情,这很丑陋:
some_test() {
# retuns nonzero if some condition is met, and zero otherwise
}
some_change() {
# makes a change that should alter the return code of the above test
}
set -e
some_test
echo "expected success, found it"
some_change
set +e
some_test # expect failure
if [[ $? -eq 0 ]]
then
echo "expected failure, found success"
exit(1)
else
echo "expected failure, found it"
fi
set -e
# ...more testing below
我正在寻找更简洁的东西。我知道我可以容忍这样的失败命令:
some_test
some_change
some_test && true
但我不想仅仅容忍失败,我想断言失败。如果第二个测试通过,我希望我的脚本失败。
我不相信使用有条件的体操来模仿这一点是可能的。