我想终止任何失败的脚本,包括在管道内。在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不退出?
编辑:添加另一个测试
我将其与其他shell进行了比较,并得到了不同的结果:
-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中的bug之外,我不明白是什么原因导致了这种行为。据曼·克什说:
-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.
发布于 2020-08-30 16:22:05
它看起来像是ksh93u+引入的回归。ksh93u在这方面发挥了预期的作用(和zsh、bash、mksh、yash和busybox一样)。
我刚刚在https://github.com/ksh93/ksh/issues/121上提出了这个问题
现在固定在https://github.com/ksh93/ksh/commit/c049eec854d506ad6110eb50c769b10224c60075中,包括从v1.0.0到beta.1
https://unix.stackexchange.com/questions/580739
复制相似问题