gpt4 book ai didi

go - 如何获取管道进程的退出代码?

转载 作者:IT王子 更新时间:2023-10-29 02:28:42 25 4
gpt4 key购买 nike

我编写了一个小型 Go 应用程序,它使用此处概述的技术从 stdin 读取:https://flaviocopes.com/go-shell-pipes/

我这样调用它:cmd | fsjl(fsjl 是我的应用)

问题是,即使 cmd 退出 1,退出代码也总是 0

如何从 cmd 中“转发”退出代码?

Here is my source code

最佳答案

这不是您的 Go 程序的工作。这是管理整个管道的人的工作。管道元素看不到彼此的退出状态。

例如,在 bash 中,您可以使用 pipefail :

If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default.

这是一个例子,using a subshellpipefail 的影响限制在一行中:

(set -o pipefail && cmd | fsjl)

你不能总是假设 bash,但是有 ways在其他 shell 中做类似的事情。

关于go - 如何获取管道进程的退出代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50978722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com