亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

管道到三通時強制線緩沖標準輸出

管道到三通時強制線緩沖標準輸出

慕俠2389804 2019-08-31 14:43:36
通常,stdout是行緩沖的。換句話說,只要您的printf參數以換行符結尾,您就可以立即打印該行。使用管道重定向時,這似乎不成立tee。我有一個C ++程序,a它輸出字符串,總是被\n終止stdout。當它由它自己運行時(./a),所有內容都在正確的時間正確打印,正如預期的那樣。但是,如果我將它傳遞給tee(./a | tee output.txt),它會在它退出之前不會打印任何內容,這會使使用的目的失效tee。我知道我可以通過fflush(stdout)在C ++程序中的每次打印操作之后添加一個來修復它。但是有更清潔,更簡單的方法嗎?例如,是否有一個命令可以運行stdout,即使使用管道也會強行進行行緩沖?
查看完整描述

3 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

嘗試unbuffer哪個是expect包的一部分。您可能已經在系統上擁有它。


在你的情況下你會像這樣使用它:


./a | unbuffer -p tee output.txt


(-p用于管道模式,其中unbuffer從stdin讀取并將其傳遞給其余參數中的命令)


查看完整回答
反對 回復 2019-08-31
?
MYYA

TA貢獻1868條經驗 獲得超4個贊

你可以試試 stdbuf


$ stdbuf -o 0 ./a | tee output.txt

(大)手冊頁的一部分:


  -i, --input=MODE   adjust standard input stream buffering

  -o, --output=MODE  adjust standard output stream buffering

  -e, --error=MODE   adjust standard error stream buffering


If MODE is 'L' the corresponding stream will be line buffered.

This option is invalid with standard input.


If MODE is '0' the corresponding stream will be unbuffered.


Otherwise MODE is a number which may be followed by one of the following:

KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

In this case the corresponding stream will be fully buffered with the buffer

size set to MODE bytes.

但要記住這一點:


NOTE: If COMMAND adjusts the buffering of its standard streams ('tee' does

for e.g.) then that will override corresponding settings changed by 'stdbuf'.

Also some filters (like 'dd' and 'cat' etc.) dont use streams for I/O,

and are thus unaffected by 'stdbuf' settings.

你沒在運行stdbuf上tee,你運行它a,所以這應該不會影響你,除非你設置的緩沖a的溪流a的源頭“。


另外,stdbuf是不是 POSIX,但GNU-的coreutils的一部分。


查看完整回答
反對 回復 2019-08-31
?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

您也可以嘗試使用命令在偽終端中執行script命令(這應該強制執行到管道的行緩沖輸出)!


script -q /dev/null ./a | tee output.txt     # Mac OS X, FreeBSD

script -c "./a" /dev/null | tee output.txt   # Linux

請注意,該script命令不會傳播回包裝命令的退出狀態。


查看完整回答
反對 回復 2019-08-31
  • 3 回答
  • 0 關注
  • 574 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號