jmd*_*_dk 5 error-handling bash shell function
如果我set -e输入了Bash脚本,该脚本将在以后出现错误时退出。我对此功能如何工作感到困惑。请考虑以下内容,这些内容仅会one按标准输出:
set -e # Exit on error
fun(){
echo one
non_existing_command
echo two
}
fun
Run Code Online (Sandbox Code Playgroud)
显然,这non_existing_command是一个错误,因此脚本在第二个脚本之前退出echo。通常,只有在第一个命令失败时,才可以使用or运算符||运行另一个命令。也就是说,我怀疑以下内容可以同时打印one和three,但不能打印出来two:
set -e # Exit on error
fun(){
echo one
non_existing_command
echo two
}
fun || echo three
Run Code Online (Sandbox Code Playgroud)
但是我得到的是one和two。也就是说,||操作员阻止退出(如应有的那样),但它选择继续使用功能主体,而忽略右手命令。
有什么解释吗?