2012-08-01 426 views
15

我想將腳本的輸出重定向到不同的程序。這是我通常會做使用這兩種形式:在Makefile規則中重定向標準輸出和stderr

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規則裏面)

+4

這應該工作。 'make'將整行傳遞給'/ bin/sh'來解釋,所以任何這個shell(不需要是你的用戶shell)都可以理解。 – 2012-08-01 08:26:11

+2

究竟是如何不起作用?嘗試在makefile中的某處設置'export SHELL:=/bin/bash'。 – 2012-08-01 08:34:26

+0

第二個命令運行,就好像它沒有收到任何來自'stdin'的輸入。它實際上在第一個之前運行。使用'||'而不是'|'來維護順序,但又一次'pyrg'沒有得到輸入。 – Xyand 2012-08-01 08:51:09

回答

3

這並不能解釋爲什麼直接的方法不起作用,但它的確有用:

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

我偶然發現了同樣的問題,並不滿意答案。我有一個二進制TLBN,在測試用例example2.TLBN上失敗。

這是我的make文件首先看到的。

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

與錯誤信息,我期待並暫停make進程失敗。

這是我的解決辦法:

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

注意-在這告訴make忽略任何輸出到stderr行的開頭。

希望這可以幫助有類似問題的人。