无法将bash标准输出重定向到文件中

时间:2022-10-16 13:58:08

I am reading 'advanced bash script', in Chapter 31, there is a problem. I can not figure it out.

我正在阅读“高级bash脚本”,在第31章,有一个问题。我搞不懂。

tail -f /var/log/msg | grep 'error' >> logfile

尾部-f /var/log/msg | grep 'error' >> logfile

Why is there nothing to output into logfile?
can you offer me an explanation? thank you in advance

为什么在日志文件中没有输出?你能给我一个解释吗?提前谢谢你

1 个解决方案

#1


3  

As @chepner comments, grep is using a larger buffer (perhaps 4k or more) to buffer its stdout. Most of the standard utilities do this when piping or redirecting to a file. They typically only switch to line-buffered mode when outputting directly to the terminal.

正如@chepner所说,grep正在使用一个更大的缓冲区(可能是4k或更多)来缓冲其stdout。大多数标准实用程序在管道传输或重定向到文件时都这样做。它们通常只在直接输出到终端时切换到行缓冲模式。

You can use the stdbuf utility to force grep to do line buffering of its output:

您可以使用stdbuf实用程序强制grep对其输出执行行缓冲:

tail -f /var/log/msg | stdbuf -oL grep 'error' >> logfile

As an easily observable demonstration of this effect, you can try the following two commands:

作为这个效果的一个很容易观察到的演示,您可以尝试以下两个命令:

for ((i=0;;i++)); do echo $i; sleep 0.001; done | grep . | cat

and

for ((i=0;;i++)); do echo $i; sleep 0.001; done | stdbuf -oL grep . | cat

In the first command, the output from grep . (i.e. match all lines) be buffered going into the pipe to cat. On mine the buffer appears to be about 4k. You will see the ascending numbers output in chunks as the buffer gets filled and then flushed.

在第一个命令中,grep的输出。(即匹配所有的线)将缓冲进入到cat管道中。我的缓冲区看起来大约为4k。当缓冲区被填充然后刷新时,您将看到以块形式输出的升序数字。

In the second command, grep's output into the pipe to cat is line-buffered, so you should see terminal output for every line, i.e. more-or-less continuous output.

在第二个命令中,grep向cat管道的输出是行缓冲的,因此您应该看到每一行的终端输出,即差不多连续的输出。

#1


3  

As @chepner comments, grep is using a larger buffer (perhaps 4k or more) to buffer its stdout. Most of the standard utilities do this when piping or redirecting to a file. They typically only switch to line-buffered mode when outputting directly to the terminal.

正如@chepner所说,grep正在使用一个更大的缓冲区(可能是4k或更多)来缓冲其stdout。大多数标准实用程序在管道传输或重定向到文件时都这样做。它们通常只在直接输出到终端时切换到行缓冲模式。

You can use the stdbuf utility to force grep to do line buffering of its output:

您可以使用stdbuf实用程序强制grep对其输出执行行缓冲:

tail -f /var/log/msg | stdbuf -oL grep 'error' >> logfile

As an easily observable demonstration of this effect, you can try the following two commands:

作为这个效果的一个很容易观察到的演示,您可以尝试以下两个命令:

for ((i=0;;i++)); do echo $i; sleep 0.001; done | grep . | cat

and

for ((i=0;;i++)); do echo $i; sleep 0.001; done | stdbuf -oL grep . | cat

In the first command, the output from grep . (i.e. match all lines) be buffered going into the pipe to cat. On mine the buffer appears to be about 4k. You will see the ascending numbers output in chunks as the buffer gets filled and then flushed.

在第一个命令中,grep的输出。(即匹配所有的线)将缓冲进入到cat管道中。我的缓冲区看起来大约为4k。当缓冲区被填充然后刷新时,您将看到以块形式输出的升序数字。

In the second command, grep's output into the pipe to cat is line-buffered, so you should see terminal output for every line, i.e. more-or-less continuous output.

在第二个命令中,grep向cat管道的输出是行缓冲的,因此您应该看到每一行的终端输出,即差不多连续的输出。