本文介绍了通过`less`命令所需的时间显示输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个产生大量输出的脚本。在点了几秒钟脚本暂停 T

I have a script that produces a lot of output. The script pauses for a few seconds at point T.

现在我使用的是命令来分析脚本的输出。
所以我执行 ./脚本|少。我离开它运行足够长的时间,这样脚本将执行完毕。

Now I am using the less command to analyze the output of the script.So I execute ./script | less. I leave it running for sufficient time so that the script would have finished executing.

现在我通过由pressing PG Down键输出少的命令。令人惊讶的,而在该点 T 的输出,我再次发现的几秒钟暂停滚动。

Now I go through the output of the less command by pressing Pg Down key. Surprisingly while scrolling at the point T of the output I notice the pause of few seconds again.

该脚本不希望任何输入,会通过绝对我开始分析少的输出的时间完成。

The script does not expect any input and would have definitely completed by the time I start analyzing the output of less.

有人能解释的几秒钟停顿是如何在较少的输出noticable当脚本会执行完毕?

Can someone explain how the pause of few seconds is noticable in the output of less when the script would have finished executing?

推荐答案

您的脚本用通过管道通信。管道是一个字节的内存流,连接两个端点:你的脚本和程序,前者写输出到它,从它后面的阅读

Your script is communicating with less via a pipe. Pipe is an in-memory stream of bytes that connects two endpoints: your script and the less program, the former writing output to it, the latter reading from it.

由于管道是内存,如果他们长大任意大,将不愉快。这样,默认,还有,可以在管内的数据的限制在任何特定的时刻(写的,但尚未读取)。默认情况下它是64K在Linux上。如果管道满,你的脚本尝试写,写块。所以的您的脚本不实际工作,就做一个写的时候停在某个点()呼叫

As pipes are in-memory, it would be not pleasant if they grew arbitrarily large. So, by default, there's a limit of data that can be inside the pipe (written, but not yet read) at any given moment. By default it's 64k on Linux. If the pipe is full, and your script tries to write to it, the write blocks. So your script isn't actually working, it stopped at some point when doing a write() call.

如何克服呢?调整默认值是一个坏的选择;什么来代替是分配在读者的缓冲器,以便它读取到缓冲区,释放管,从而让写程序的工作,但示出了您(或把手)只有输出的一部分。 有这样一个缓冲区,在默认情况下,扩大它会自动,但是,它并没有在背景填充它,就如同读取输入它只是填充它。

How to overcome this? Adjusting defaults is a bad option; what is used instead is allocating a buffer in the reader, so that it reads into the buffer, freeing the pipe and thus letting the writing program work, but shows to you (or handles) only a part of the output. less has such a buffer, and, by default, expands it automatically, However, it doesn't fill it in the background, it only fills it as you read the input.

那么,将解决你的问题是读取文件,直到最后(就像你通常会preSS ),然后回到开头(就像你通常会preSS 先按g )。问题是,你可以通过类似这样的命令行指定以下命令:

So what would solve your problem is reading the file until the end (like you would normally press ), and then going back to the beginning (like you would normally press ). The thing is that you may specify these commands via command line like this:

./script | less +Gg

您应该注意,但是,你将不得不等待,直到整个脚本的输出加载到内存中,所以您将无法同时观看。 是不够成熟的。但是,如果这就是你真正需要(浏览输出的开始,而 ./脚本仍在计算的结尾),您可能需要使用临时文件:

You should note, however, that you will have to wait until the whole script's output loads into memory, so you won't be able to view it at once. less is insufficiently sophisticated for that. But if that's what you really need (browsing the beginning of the output while the ./script is still computing its end), you might want to use a temporary file:

 ./script >x & less x ; rm x

这篇关于通过`less`命令所需的时间显示输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 09:19