如何使任何 shell 命令的输出无缓冲? [英] How to make output of any shell command unbuffered?
问题描述
有没有办法在没有输出缓冲的情况下运行 shell 命令?
Is there a way to run shell commands without output buffering?
例如,hexdump 文件 |./my_script
只会将输入从 hexdump 以缓冲块的形式传递给 my_script,而不是逐行传递.
For example, hexdump file | ./my_script
will only pass input from hexdump to my_script in buffered chunks, not line by line.
实际上我想知道如何使任何命令无缓冲的通用解决方案?
Actually I want to know a general solution how to make any command unbuffered?
推荐答案
AFAIK,如果没有丑陋的黑客,你就无法做到.写入管道(或从中读取)会自动打开完整缓冲,您对此无能为力:-(.行缓冲"(这是您想要的)仅在读取/写入终端时使用.丑陋的黑客正是这样做的:他们将程序连接到伪终端,以便管道中的其他工具以行缓冲模式从该终端读/写.整个问题描述如下:
AFAIK, you can't do it without ugly hacks. Writing to a pipe (or reading from it) automatically turns on full buffering and there is nothing you can do about it :-(. "Line buffering" (which is what you want) is only used when reading/writing a terminal. The ugly hacks exactly do this: They connect a program to a pseudo-terminal, so that the other tools in the pipe read/write from that terminal in line buffering mode. The whole problem is described here:
该页面还有一些建议(前面提到的丑陋的黑客")该怎么做,即使用 unbuffer
或使用 LD_PRELOAD
拉一些技巧.
The page has also some suggestions (the aforementioned "ugly hacks") what to do, i.e. using unbuffer
or pulling some tricks with LD_PRELOAD
.
这篇关于如何使任何 shell 命令的输出无缓冲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!