use*_*352 0 linux bash shell scripting
我希望能够在运行时以十六进制看到我的程序的输出,但是如果我将输出通过管道传输到 xxd 它只会在程序停止运行后打印。
这些是我尝试过的事情:
./a.out | while read LINE; do echo "$LINE" | xxd; done
./a.out | xxd
Run Code Online (Sandbox Code Playgroud)
这是我的控制台的样子:(箭头是我的评论)
1 <-- input
2 <-- input
3 <-- input
4 <-- input
a <-- input (program ends after this input)
00000000: 6465 6667 0a defg.. <-- output
00000000: 6465 6667 0a defg.. <-- output
00000000: 6465 6667 0a defg.. <-- output
00000000: 6465 6667 0a defg.. <-- output
Run Code Online (Sandbox Code Playgroud)
但这就是我想要的
1 <-- input
00000000: 6465 6667 0a defg.. <-- output
2 <-- input
00000000: 6465 6667 0a defg.. <-- output
3 <-- input
00000000: 6465 6667 0a defg.. <-- output
4 <-- input
00000000: 6465 6667 0a defg.. <-- output
a <-- input (program ends after this input)
Run Code Online (Sandbox Code Playgroud)
(这是一个简单的测试程序,如果输入数字,它将打印一个设置的字符串,否则它将退出)
交流节目
#include <stdio.h>
int main() {
const char test[7] = {0x64,0x65,0x66,0x67,'\n',0x00};
int testInteger;
while(scanf("%d", &testInteger)){
printf(test);
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
默认情况下,stdout写入管道时完全缓冲,因此read在刷新输出之前不会接收任何内容。在程序写入 4K 字节(近 700 行)或退出之前,这不会发生。
您可以使用stdbuf来改变缓冲。
stdbuf -oL a.out | xxd
Run Code Online (Sandbox Code Playgroud)
-oL切换到行缓冲,因此xxd(或任何其他程序)将在打印时接收每一行。
| 归档时间: |
|
| 查看次数: |
81 次 |
| 最近记录: |