bash 如果结果不仅仅是我的外壳上的一页,如何自动管道到更少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4174327/
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 automatically pipe to less if the result is more than a page on my shell?
提问by Lazer
Mostly, I will not use | lessfor each and every command from the shell.
大多数情况下,我不会使用| lessshell 中的每个命令。
Pipe to less is used only when I actually run the command without is and find out that it does not fit on the page. That costs me two runs of the same shell command.
仅当我实际运行没有 is 的命令并发现它不适合页面时,才使用管道到 less。这花费了我两次运行相同的 shell 命令。
Is there a way so that every time a command result is more than a display page, it automatically gets piped to less?
有没有办法让每次命令结果不仅仅是显示页面时,它会自动通过管道传输到更少?
采纳答案by Paused until further notice.
The most significant problem with trying to do that is how to get it to turn off when running programs that need a tty.
尝试这样做的最大问题是如何在运行需要 tty 的程序时关闭它。
What I would recommend is that, for programs and utilities you frequently use, create shell functions that wrap them and pipe to less -F. In some cases, you can name the function the same as the program and it will take precedence, but can be overridden.
我的建议是,对于您经常使用的程序和实用程序,创建将它们包装起来并通过管道传输到less -F. 在某些情况下,您可以将函数命名为与程序相同的名称,它会优先,但可以被覆盖。
Here is an example wrapper function which would need testing and perhaps some additional code to handle edge cases, etc.
这是一个示例包装函数,它需要测试以及一些额外的代码来处理边缘情况等。
#!/bin/bash
foo () {
if [[ -p /dev/stdout ]] # you don't want to pipe to less if you're piping to something else
then
command foo "$@" | less -F
else
command foo "$@"
fi
}
If you use the same name as I have in the example, it could break things that expect different behavior. To override the function to run the underlying program directly precede it with command:
如果您使用的名称与我在示例中的名称相同,则可能会破坏预期不同行为的内容。要覆盖函数以直接在其前面运行基础程序command:
command foo
will run foowithout using the function of the same name.
将在foo不使用同名函数的情况下运行。
回答by Ben Hymanson
Pipe it to less -Faka --quit-if-one-screen:
管道它less -F又名--quit-if-one-screen:
Causes less to automatically exit if the entire file can be dis- played on the first screen.
如果整个文件可以显示在第一个屏幕上,则使 less 自动退出。
回答by GWW
You could always pipe to less -E (this will cause less to automatically quit at the end of the file). For commands with short output it would do what you want. I don't think you can automatically pipe to less when there is a lot of output.
您始终可以通过管道传递到 less -E(这将导致 less 在文件末尾自动退出)。对于输出短的命令,它会做你想做的。我不认为当有很多输出时你可以自动管道到更少。
回答by Jonathan Leffler
In general, automatically piping to lessrequires the shell to be prescient about the output that will be produced by the commands it runs - and it is hard enough for humans to predict that without trying to make programs do so.
一般来说,自动管道到less要求 shell 对其运行的命令将产生的输出有先见之明——人类很难预测这一点,而无需试图让程序这样做。
You could write a shell that does it for you - that captures the output (but what about stderr?) and paginates if necessary, but it would most certainly not be a standard shell.
您可以编写一个为您执行此操作的外壳程序 - 捕获输出(但标准错误呢?)并在必要时进行分页,但它肯定不是标准外壳程序。
回答by Jonathan
I wrote this wrapper function and put it in my .profile. You can use this before a command and it will automatically pipe it to less if it is longer than 1 page.
我写了这个包装函数并将它放在我的 .profile 中。您可以在命令之前使用它,如果它长于 1 页,它会自动将其通过管道传输到 less。
lcmd ()
{
echo "$("$@")" | less -F;
};
So 'lcmd ls' would ls the current directory and pipe that output to less.
所以 'lcmd ls' 将 ls 当前目录和管道输出到更少。

