bash 如何用shell命令刷新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17529810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to flush with shell command
提问by MOHAMED
are there an equivalent of the C function fflush()for shell command?
是否有相当于fflush()shell 命令的 C 函数?
echo "kkkk"
flush <<< are there some command to execute to flush stdout?
回答by dash-o
Short answer: There is no need to perform explicit flush for shell scripts. All shell output (via echo) is NOT buffered. The output is immediately sent to the output device. The situation is more complex if the output is piped into another program, which may buffer it's output.
简短回答:无需对 shell 脚本执行显式刷新。所有 shell 输出(通过 echo)都没有缓冲。输出立即发送到输出设备。如果将输出通过管道传输到另一个程序中,则情况会更加复杂,这可能会缓冲它的输出。
Long answer: consider
长答案:考虑
(echo aaa ; sleep 3 ; echo bbb)
Which will display 'aaa', and after 3 seconds delay, 'bbb' - since the pipe construct will result in the the C program using buffered output.
它将显示“aaa”,并在 3 秒延迟后显示“bbb”——因为管道构造将导致 C 程序使用缓冲输出。
As noted above, if the output is send (via pipe) to a program which will buffer the output, the first line may be delayed, and displayed at the same time as the second line (after the 3 seconds delay). The solution is to force "line buffering" (or no buffering) on other programs in the pipeline.
如上所述,如果将输出(通过管道)发送到将缓冲输出的程序,则第一行可能会延迟,并与第二行同时显示(延迟 3 秒后)。解决方案是对管道中的其他程序强制执行“行缓冲”(或无缓冲)。
For example given a simple "C" program to copy stdin to stdout:
例如,给定一个简单的“C”程序将标准输入复制到标准输出:
int main() { char b[256] ; while (fgets(b, sizeof(b), stdin) ) fputs(b, stdout) };
and running
和跑步
(echo aaa ; sleep 3 ; echo bbb ) | a.out | cat
Will result in buffering in a.out, showing 'aaa' and 'bbb' after 3 seconds delay.
将导致 a.out 中的缓冲,在 3 秒延迟后显示 'aaa' 和 'bbb'。
回答by ams
No, you should never need it.
不,你永远不需要它。
When a program, such as echo, exits, all output data is automatically flushed. If the program has not exited yet then flushing is an internal matter and nothing to do with the shell.
当程序(例如echo)退出时,所有输出数据都会自动刷新。如果程序尚未退出,则刷新是内部问题,与外壳无关。

