How to make output of any shell command unbuffered?

后端 未结 4 2022
天命终不由人
天命终不由人 2020-11-22 16:25

Is there a way to run shell commands without output buffering?

For example, hexdump file | ./my_script will only pass input from hexdump to my_script in

4条回答
  •  广开言路
    2020-11-22 17:06

    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:

    • http://www.pixelbeat.org/programming/stdio_buffering/

    The page has also some suggestions (the aforementioned "ugly hacks") what to do, i.e. using unbuffer or pulling some tricks with LD_PRELOAD.

提交回复
热议问题