【问题标题】:Piping stdout and stderr inside a Makefile rule在 Makefile 规则中管道 stdout 和 stderr
【发布时间】:2012-07-30 02:48:19
【问题描述】:

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

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

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

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

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

编辑:

这似乎是一个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

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

【问题讨论】:

  • 这应该可以。 make 将整行传递给/bin/sh 进行解释,所以这个shell(不需要是你的用户shell)可以理解的任何东西。
  • 到底怎么不工作?尝试在 makefile 中的某处设置export SHELL := /bin/bash
  • 第二个命令运行时好像没有收到来自stdin 的任何输入。它实际上在第一个之前运行。使用|| 而不是| 可以保持顺序,但pyrg 再次没有得到输入。
  • @MaximYegorushkin,此设置有助于避免在使用|& 时出现错误。但它的行为仍然与我之前的评论一样。
  • 程序的启动顺序无关紧要。即便如此,两者也会通过管道连接起来。

标签: linux bash shell makefile pipe


【解决方案1】:

它没有解释为什么简单的方法不起作用,但它确实有效:

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

【讨论】:

    【解决方案2】:

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

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

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

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

    这是我的修复:

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

    注意行首的-,它告诉make忽略任何到stderr的输出。

    希望这对遇到类似问题的人有所帮助。

    【讨论】:

      【解决方案3】:

      奇怪的是,我遇到了同样的问题,然后这样解决了:

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

      我不太确定为什么 2&gt;&amp;1 &gt;errors.txt 在这里不起作用,但 &amp;&gt; 起作用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-21
        • 2023-04-05
        • 1970-01-01
        • 2020-09-26
        相关资源
        最近更新 更多