Rab*_*ski 4 gnu-screen terminal
我有一个基于 debian 的系统(实际上是 Voyage Linux 0.9.1,它基于 debian wheezy)并且当我这样做时/etc/init.d/ntp status
它返回(例如)
$ /etc/init.d/ntp status
[ ok ] NTP server is running
Run Code Online (Sandbox Code Playgroud)
但是,在 screen 会话(版本 4.01.00devel 2-May-06)[ ok ]
中没有显示。
$ screen
$ /etc/init.d/ntp status
NTP server is running
Run Code Online (Sandbox Code Playgroud)
为什么会这样?以及如何在 Screen 会话中显示它?
BTW:在另一台Debian服务器上(这也将返回xterm-256color
当我这样做echo $TERM
),我注意到,/etc/init.d/ntp status
它永远不会显示的[ ok ]
,不管我是否在屏幕会话或没有,所以它可能不是一个真正的screen
问题。
它不需要屏幕。我们来实验一下:
TERM=dumb /etc/init.d/ntp status # no [OK]
/etc/init.d/ntp status | cat # also no [OK]
Run Code Online (Sandbox Code Playgroud)
这也排除了[OK]
. 由于显然输出根据 TERM 设置(dumb
并screen
删除[OK]
)而有所不同,因此您可以假装拥有功能齐全的终端而不是屏幕:
TERM=xterm /etc/init.d/ntp status
Run Code Online (Sandbox Code Playgroud)
它似乎也可以检测它是否连接到终端。当您将输出通过管道传输到另一个进程时,这些信息就会[OK]
消失。
我认为这样做的原因是由于使用了转义码[OK]
,有一个检查可以防止它们在“未保存”环境中使用。转义序列并没有真正添加任何东西,但它们可能会弄乱输出。
终端能力检查在/lib/lsb/init-functions
. 有一个函数调用log_use_fancy_output
它使用tput
:
tput
tput 实用程序使用 terminfo 数据库使依赖于终端的功能和信息的值对 shell 可用
它发出以下两个命令:
tput hpa 60
tput setaf 1
Run Code Online (Sandbox Code Playgroud)
来自tput:便携式终端控制和Bash 提示 HOWTO - 颜色和光标移动:
hpa - Move cursor to column #1
setaf [1-7] - Set a foreground colour using ANSI escape
Run Code Online (Sandbox Code Playgroud)
特别是log_use_fancy_output
检查它是否连接到终端。然后检查 TERM 是否为空或设置为哑,以及这两个tput
操作是否成功。如果成功,[OK]
则打印 ,否则[OK]
跳过 。