0

Continuingサブシェルを削除すると、以下のコードが出力されます。サブシェルでは、テスト後に別の成功した呼び出しが必要です (:成功した no-op コマンドを使用するのが最も簡単です、IMO) Continuing

#!/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

この動作は正しいですか? サブシェルを導入すると動作が変わるのはなぜですか

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

私はdash 0.5.7-2ubuntu2これを実行するために使用しています。

4

1 に答える 1

1

This is expected. set -e ignores a non-zero exit status from an AND-list, but not from a subshell. The difference between

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

and

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

is that in the former, your script sees an AND-list with a non-zero exit status, but in the latter it sees a subshell with a non-zero exit status.

于 2015-09-10T15:13:19.047 に答える