首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >设置-eo管道故障在ksh和bash上不同

设置-eo管道故障在ksh和bash上不同
EN

Unix & Linux用户
提问于 2020-04-17 16:27:25
回答 1查看 8.5K关注 0票数 5

我想终止任何失败的脚本,包括在管道内。在bash中,您可以使用set -eo pipefail,但这在ksh中不起作用。

示例:

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

为什么在这种情况下ksh不退出?

编辑:添加另一个测试

我将其与其他shell进行了比较,并得到了不同的结果:

代码语言:javascript
运行
复制
-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之外,我不明白是什么原因导致了这种行为。据曼·克什说:

代码语言:javascript
运行
复制
-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.
EN

回答 1

Unix & Linux用户

回答已采纳

发布于 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

票数 3
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/580739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档