开发者

can program know return value of previous command in pipe chain?

开发者 https://www.devze.com 2023-03-20 05:43 出处:网络
If I have progA | progB, then can I make some progB executable (in any language) which will know the return code of p开发者_StackOverflow中文版rogA?

If I have progA | progB, then can I make some progB executable (in any language) which will know the return code of p开发者_StackOverflow中文版rogA?

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.


I'm pretty sure the answer is no for POSIX. The two main arguments against it are:

  • there is no POSIX way for progB to determine the PID of the progA 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).
  • even if progB had the PID of progA, it could not receive its exit status since only the parent process of progA (in this case probably your shell) can wait for it. As far as I can tell, there is no way around this restriction.
0

精彩评论

暂无评论...
验证码 换一张
取 消