How to make output of any shell command unbuffered?

后端 未结 4 1984
天命终不由人
天命终不由人 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条回答
  •  旧时难觅i
    2020-11-22 16:59

    You could also use the script command to make the output of hexdump line-buffered (hexdump will be run in a pseudo terminal which tricks hexdump into thinking its writing its stdout to a terminal, and not to a pipe).

    # cf. http://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe/
    stty -echo -onlcr
    script -q /dev/null hexdump file | ./my_script         # FreeBSD, Mac OS X
    script -q -c "hexdump file" /dev/null | ./my_script    # Linux
    stty echo onlcr
    

提交回复
热议问题