我正在编写自动化脚本 (perl
/bash
)。他们中的许多人受益于一些基本的终端 GUI。我想我会使用标准的 ANSI
序列来进行基本绘图。在终端中绘图之前,我执行了 clear
但这样做我丢失了一些终端命令历史记录。当我的程序存在时,我希望能够恢复终端命令历史记录。许多终端程序(例如 less
、man
、vim
、htop
、nmon
、 whiptail
、dialog
等)正是这样做的。所有这些都可以恢复终端窗口,将用户带回调用程序之前的位置,并显示之前执行的所有命令的历史记录。
老实说,我什至不知道从哪里开始搜索。它是来自 curses
库的命令吗?它是 ANSI
转义序列吗?我应该弄乱 tty
吗?我被卡住了,任何指示都会非常有帮助。
编辑:我想澄清一下,我并不是真的在问“如何使用备用屏幕”。我正在寻找一种方法来保存终端命令历史记录。我的问题的一个可能答案是“使用替代屏幕”。 “什么是替代屏幕以及如何使用它”这个问题是一个不同的问题,而这个问题反过来已经在别处发布了答案。谢谢:)
最佳答案
您应该使用备用屏幕 终端功能。看 Using the "alternate screen" in a bash script
对“如何使用备用屏幕”的回答:
这个例子应该说明:
#!/bin/sh
: <<desc
Shows the top of /etc/passwd on the terminal for 1 second
and then restores the terminal to exactly how it was
desc
tput smcup #save previous state
head -n$(tput lines) /etc/passwd #get a screenful of lines
sleep 1
tput rmcup #restore previous state
这仅适用于具有 smcup
和 rmcup
功能的终端(例如,不适用于 Linux 控制台(= 虚拟控制台))。
可以使用 infocmp
检查终端功能。
在不支持它的终端上,我的tput smcup
只是返回退出状态 1,而不输出转义序列。
注意:
如果您打算重定向输出,您可能希望将转义序列直接写入 /dev/tty
,以免弄脏您的 stdout
:
exec 3>&1 #save old stdout
exec 1>/dev/tty #write directly to terminal by default
#...
cat /etc/passwd >&3 #write actual intended output to the original stdout
#...
关于linux - 保存和恢复终端内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33929816/