0

Continuing如果我删除子外壳,下面的代码会打印出来。:使用 subshel​​l ,如果我想进入该Continuing部分,我需要在测试后再次成功调用(使用成功的 no-op 命令是最直接的,IMO)。

#!/bin/sh
set -e #Exit on untested error
( #Subshell
    #Some succesfful commands go here
    #And here comes a file test
    [ -f "doesntExist" ] && {
        : #Irrelevant
    }
    #: 
)
echo Continuing

这种行为正确吗?为什么引入子shell会改变

[ -f "doesntExist" ] && {
      : 
}

我正在使用dash 0.5.7-2ubuntu2来运行它。

4

1 回答 1

1

这是意料之中的。set -e忽略 AND 列表中的非零退出状态,但不忽略子 shell 中的退出状态。和...之间的不同

set -e
[ -f "doesntExist" ] && {
    : #Irrelevant
}
echo Continuing

set -e
( [ -f "doesntExist" && { : ; } )
echo Continuing

是在前者中,您的脚本看到一个具有非零退出状态的 AND 列表,但在后者中,它看到一个具有非零退出状态的子 shell。

于 2015-09-10T15:13:19.047 回答