Linux 在 Makefile 规则中管道 stdout 和 stderr

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11755312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:59:11  来源:igfitidea点击:

Piping stdout and stderr inside a Makefile rule

linuxbashshellmakefilepipe

提问by Xyand

I want to pipe the output of a script to a different program. Something I would normally do using these two forms:

我想将脚本的输出通过管道传输到不同的程序。我通常会使用这两种形式做的事情:

 python test.py 2>&1 | pyrg
 python test.py |& pyrg

My problem is that it doesn't work from inside a makefile:

我的问题是它在 makefile 中不起作用:

[Makefile]
test:
    python test.py 2>&1 | pyrg [doesn't work]

I wish to avoid writing a script file that does the work.

我希望避免编写完成工作的脚本文件。

Edit:

编辑:

This seems like a pyrgissue:

这似乎是一个pyrg问题:

python test.py 2>&1 | tee test.out // Writes to the file both stderr and stdout
cat test.out | pyrg                // Works fine!
python test.py 2>&1 | pyrg         // pyrg behaves as if it got no input

This is a bad solution for me as I never get to the catpart in case of a test failure (everything is inside a Makefile rule)

这对我来说是一个糟糕的解决方案,因为cat在测试失败的情况下我永远不会参与其中(一切都在 Makefile 规则中)

采纳答案by Xyand

It doesn't explain why the straightforward approaches don't work, but it does the trick:

它没有解释为什么直截了当的方法不起作用,但它确实有效:

[Makefile]
test: 
    python test.py >test.out 2>&1; pyrg <test.out

回答by Lucas

I stumbled upon this question with the same problem and wasn't satisfied with the answer. I had a binary TLBNthat failed on test case example2.TLBN.

我偶然发现了这个问题,但对答案并不满意。我有一个TLBN在测试用例上失败的二进制文件example2.TLBN

This is what my make file looked at first.

这是我的 make 文件首先查看的内容。

make:
     ./TLBN example2.TLBN > ex2_output.txt

Which failed with the error message I was expecting and halting the make process.

失败并显示我期望的错误消息并停止制作过程。

This is my fix:

这是我的修复:

make:
    -./TLBN example2.TLBN > ex2_output.txt 2>&1

Note the -at the beginning of the line which tells make to ignore any output to stderr.

请注意该-行的开头,它告诉 make 忽略对 stderr 的任何输出。

Hope this helps someone that has a similar problem.

希望这可以帮助有类似问题的人。

回答by user5359531

Strangely, I had the same problem, and solved it like this:

奇怪的是,我遇到了同样的问题,并像这样解决了它:

check-errors:
    check-for-errors.sh &> errors.txt

I am not really sure why 2>&1 >errors.txtdid not work here, but &>did

我不确定为什么2>&1 >errors.txt在这里不起作用,但&>确实如此