程序可以知道管链中前一个命令的返回值吗?
如果我有 progA | progB,那么我可以制作一些 progB 可执行文件(任何语言),它会知道 progA
的返回码吗?
编辑:
如果答案取决于平台,那么我想知道所有平台的答案,因为这是一个纯粹出于好奇的问题。然而,POSIX 机器(尤其是 Linux)是我最常用的。
If I have progA | progB
, then can I make some progB executable (in any language) which will know the return code of progA
?
EDIT:
if the answer is platform dependent, then I would like to know the answers for all platforms since this is a question out of sheer curiousity. However, POSIX machines (linux in particular) are what I use most.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很确定 POSIX 的答案是否定的。反对它的两个主要论点是:
progB
没有 POSIX 方式来确定管道另一端progA
进程的 PID(尽管有系统)解决此问题的相关方法 - 在 Linux 上,您可以使用管道的 inode 编号来查找在大多数情况下保持另一端打开的进程,这个问题将帮助您开始)。progB
的 PID 为progA
,它也无法接收其退出状态,因为只有progA
的父进程(在这种情况下可能是您的shell)可以等待
它。据我所知,没有办法绕过这个限制。I'm pretty sure the answer is no for POSIX. The two main arguments against it are:
progB
to determine the PID of theprogA
process on the other side of the pipe (although there are system-dependent ways around this - on Linux, you can use the pipe's inode number to find the process keeping the other end open in most cases, this question will get you started).progB
had the PID ofprogA
, it could not receive its exit status since only the parent process ofprogA
(in this case probably your shell) canwait
for it. As far as I can tell, there is no way around this restriction.