短时间后自动退出少

k-h*_*k-h 0 less

我使用 less 查看包含敏感信息的命令的文本输出。少有好处,因为它使用备用屏幕并在使用后擦拭。我希望较少的会话在短时间内退出,比如 5 分钟。

是否有任何简单的命令行方法可以使用管道文本调用 less 并在 5 分钟后自动退出?

Fox*_*Fox 5

假设您有 GNU coreutils,一个稍微简单的方法是在这样的敏感命令中替换lesstimeout --foreground 600 less; printf '\033[?47h'; clear; printf '\033[?1049l'; stty cooked echo。该timeout命令在给定的持续时间后终止进程,--foreground开关允许less使用 TTY,并stty cooked echo在终止后修复终端less。如果杀死less阻止了备用屏幕的清除和退出,则干预命令会这样做。

printf命令中使用的转义序列适用于与DEC 兼容的终端(仿真器),例如 Xterm。您的特定终端可能会使用不同的序列来完成此任务。

一个简单的函数:

tless () {
  timeout --foreground 600 less "$@"
  printf '\033[?47h' # Enter alternate screen
  clear
  printf '\033[?1049l' # Exit alternate screen and restore cursor
  </dev/tty stty cooked echo # Use in a pipe requires specifying the TTY
}
Run Code Online (Sandbox Code Playgroud)

可以大大减少打字。


感谢@meuh 指出原始版本可能无法清除或退出备用屏幕。

编辑以允许tless some-file. 此外,该版本已经在 Linux 和 Solaris 11 上进行了测试。