I use less to view the text output of a command that has sensitive information. Less is good for this as it uses an alternate screen and wipes it after use. I'd like the less session to quit after a short time, like say 5 minutes.
Is there any simple command line way to invoke less with piped text and have it quit automatically after say 5 minutes?
Assuming you have GNU coreutils, a somewhat simple approach is to replace less
with timeout --foreground 600 less; printf '\033[?47h'; clear; printf '\033[?1049l'; stty cooked echo
in such sensitive commands. The timeout
command kills a process after a given duration, the --foreground
switch allows less
to use the TTY, and stty cooked echo
fixes the terminal after killing less
. In case killing less
prevented the clear and exit of the alternate screen, the intervening commands do that.
The escape sequences used in the printf
commands are appropriate for a DEC-compatible terminal (emulator) such as Xterm. Your specific terminal may use different sequences to accomplish this task.
A simple function:
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
}
can make for substantially less typing.
Thanks @meuh for pointing out that the original version might not clear or exit the alternate screen.
Edited to allow tless some-file
. Further, this version has been tested on Linux and Solaris 11.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다