本文介绍了如何自动管道少,如果结果是比我壳页面更?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数情况下,我不会使用 |少从外壳的每一个命令。

Mostly, I will not use | less for each and every command from the shell.

管少只用于不就是当我真正运行的命令,并找出它不适合在页面上。 这花了我同样的shell命令的两分。

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 there a way so that every time a command result is more than a display page, it automatically gets piped to less?

推荐答案

,试图做到这一点,最显著的问题是如何得到它的运行需要一个tty程序时关闭。

The most significant problem with trying to do that is how to get it to turn off when running programs that need a tty.

我会建议是,对于程序和实用程序,你经常使用,创建外壳函数,它们和管道换到少-F 。在某些情况下,你可以命名功能一样的程序,它会采取precedence,但可以覆盖。

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.

下面是需要测试,也许一些额外的code来处理边缘情形为例包装功能等。

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
}

如果您使用相同的名称,因为我有在这个例子中,它可以打破的东西,希望不同的行为。要覆盖功能以运行底层程序直接precede它命令

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 foo

将运行不使用相同名称的功能。

will run foo without using the function of the same name.

这篇关于如何自动管道少,如果结果是比我壳页面更?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:05