set -eoパイプラインfailはkshとbashで異なります。

set -eoパイプラインfailはkshとbashで異なります。

パイプラインの内部エラーを含むすべてのエラーが発生した場合は、スクリプトを終了したいと思います。これはbashでは機能しますset -eo pipefailが、kshでは機能しません。

例:

# x
set -eo pipefail
false | true
echo "done: $?" # reached in ksh, unexpected
false
echo "done2" # not reached in either shell, as expected
bash x        # prints nothing, as expected
ksh x         # done: 1
ksh --version # ... 93u+ 2012-08-01

この場合、kshが終了しないのはなぜですか?

編集:別のテストを追加しました

他のシェルと比較して異なる結果が得られた。

-bash-5.0$ zsh -c 'set -eo pipefail; false | true; exit 2' ; echo $?
1
-bash-5.0$ ksh -c 'set -eo pipefail; false | true; exit 2'; echo $?
2
-bash-5.0$ bash -c 'set -eo pipefail; false | true; exit 2' ; echo $?
1

kshのバグに加えて、この動作の原因が何であるか理解していません。 man kshによると:

-e    Unless contained in a ⎪⎪ or && command, or the command
      following an if while or until command or in the
      pipeline following !, if a command has a non-zero exit  # Pipeline does not follow !
      status, execute the ERR trap, if set, and exit.  This   # Pipeline has a non-zero exit status
      mode is disabled while reading profiles.

pipefail
      A pipeline will not complete until all
      components of the pipeline have completed, and
      the return value will be the value of the last
      non-zero command to fail or zero if no command
      has failed.

ベストアンサー1

ksh93u+によって導入された回帰のように見えます。 ksh93uはこの点で期待どおりに動作します(zsh、bash、mksh、yash、busybox shと共に)。

私はこの質問をしました。https://github.com/ksh93/ksh/issues/121

今固定https://github.com/ksh93/ksh/commit/c049eec854d506ad6110eb50c769b10224c60075、v1.0.0-beta.1から含まれています

おすすめ記事