如果您使用的是 xterm,那么您可以询问它使用的是什么模式。尽管备用屏幕功能是 xterm 功能而不是“DEC”,但该设置与XTerm 控制序列DECSET
中描述的模式组合在一起:
CSI ? Pm h
DEC Private Mode Set (DECSET).
...
Ps = 1 0 4 7 -> Use Alternate Screen Buffer, xterm. This
may be disabled by the titeInhibit resource.
Ps = 1 0 4 8 -> Save cursor as in DECSC, xterm. This may
be disabled by the titeInhibit resource.
Ps = 1 0 4 9 -> Save cursor as in DECSC, xterm. After sav-
ing the cursor, switch to the Alternate Screen Buffer, clear-
ing it first. This may be disabled by the titeInhibit
该DECRQM
控件可用于查询终端:
CSI ? Ps$ p
Request DEC private mode (DECRQM). For VT300 and up, reply
DECRPM is
CSI ? Ps; Pm$ y
where Ps is the mode number as in DECSET/DECSET, Pm is the
mode value as in the ANSI DECRQM.
也就是说,您的脚本可以
printf '\033[?1049$p'
并读回结果,期待类似的东西\033[?1049;1$y
这是一个快速演示:
#!/bin/sh
unexpected() {
result=$(echo "$check"|sed -e 's/^@/\\033/')
printf '? unexpected reply: %s\n' "$result"
exit 1
}
exec </dev/tty
old=`stty -g`
stty raw -echo min 0 time 5
printf '\033[?1049$p'
read status
stty $old
if [ -n "$status" ]
then
check=$(echo "$status" |tr '\033' '@')
if [ "$check" != "$status" ]
then
case "$check" in
'@[?1049;1$y')
echo "alternate screen"
;;
'@[?1049;2$y')
echo "normal screen"
;;
*)
unexpected
;;
esac
else
unexpected
fi
else
echo "? no reply from terminal"
fi
当然,如果您不使用 xterm,您的里程可能会有所不同......