如何将一个函数作为“已测试的命令”运行,并在失败时执行操作,同时尽快中止该函数?请参考以下脚本。
我得到的输出是:
我想我正在寻找的是将函数标记为未经测试的命令的方法。根据bash man页面。
#!/bin/bash -e
function foo() {
echo Entering foo
false
echo Should not reach this line
}
foo || echo I want to see this line on failure in foo
foo
我得到的输出是:
Entering foo
Should not reach this line
Entering foo
虽然我希望得到
Entering foo
I want to see this line on failure in foo
Entering foo
我想我正在寻找的是将函数标记为未经测试的命令的方法。根据bash man页面。
-e errexit
Exit immediately if any untested command fails in non-interactive
mode. The exit status of a command is considered to be explicitly
tested if the command is part of the list used to control an if,
elif, while, or until; if the command is the left hand operand of
an “&&” or “||” operator; or if the command is a pipeline preceded
by the ! operator. If a shell function is executed and its exit
status is explicitly tested, all commands of the function are con‐
sidered to be tested as well.
编辑 期望的输出是错误的。已修改以更清晰。