00. 目录
01. 命令概述
tput命令将通过 terminfo 数据库对您的终端会话进行初始化和操作。通过使用 tput,您可以更改几项终端功能,如移动或更改光标、更改文本属性,以及清除终端屏幕的特定区域。
02. 命令格式
用法:
tput [-Ttype] capname [parms ... ]
tput [-Ttype] init
tput [-Ttype] reset
tput [-Ttype] longname
tput -S <<
tput -V
(1)字符串输出参数设置
bel 警铃
blink 闪烁模式
bold 粗体
civis 隐藏光标
clear 清屏
cnorm 不隐藏光标
cup 移动光标到屏幕位置(x,y)
el 清除到行尾
ell 清除到行首
smso 启动突出模式
rmso 停止突出模式
smul 开始下划线模式
rmul 结束下划线模式
sc 保存当前光标位置
rc 恢复光标到最后保存位置
sgr0 正常屏幕
rev 逆转视图
(2)数字输出参数设置
cols 列数目
ittab 设置宽度
lines 屏幕行数
(3)布尔输出参数设置
chts 光标不可见
hs 具有状态行
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
03. 光标属性
在 UNIX shell 脚本中或在命令行中,移动光标或更改光标属性可能是非常有用的。有些情况下,您可能需要输入敏感信息(如密码),或在屏幕上两个不同的区域输入信息。在此类情况下,使用 tput 可能会对您有所帮助。
3.1 清屏
[deng@localhost ~]$ tput clear
- 1
3.2 保存当前光标位置
[deng@localhost ~]$ tput sc
[deng@localhost ~]$
- 1
- 2
3.3 将光标移动到指定行列
[deng@localhost ~]$ tput cup 10 14
[deng@localhost ~]$
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
3.4 光标不可见
[deng@localhost ~]$ tput civis
[deng@localhost ~]$
- 1
- 2
3.5 设置光标可见
[deng@localhost ~]$ tput cnorm
[deng@localhost ~]$
- 1
- 2
3.6 显示输出
[deng@localhost ~]$ tput rc
[deng@localhost ~]$
- 1
- 2
04. 移动光标
使用 tput 可以方便地实现在各设备上移动光标的位置。通过在 tput 中使用 cup 选项,或光标位置,您可以在设备的各行和各列中将光标移动到任意 X 或 Y 坐标。设备左上角的坐标为 (0,0)。
要在设备上将光标移动到第 5 列 (X) 的第 1 行 (Y),只需执行 tput cup 5 1。另一个示例是 tput cup 23 45,此命令将使光标移动到第 23 列上的第 45 行。
4.1 移动光标,执行用于显示信息的命令,然后返回到前一光标位置
[deng@localhost ~]$ (tput sc ; tput cup 5 8 ; echo “Input from tput/echo at 23/45” ; tput rc)
[deng@localhost ~]$
“Input from tput/echo at 23/45”
- 1
- 2
- 3
- 4
- 5
分析:
tput sc
必须首先保存当前的光标位置。要保存当前的光标位置,请包括 sc 选项或“save cursor position”。
tput cup 5 8
在保存了光标位置后,光标坐标将移动到 (5, 8)。
echo “Input from tput/echo at 23/45”
将信息显示到 stdout 中。
tput rc
在显示了这些信息之后,光标必须返回到使用 tput sc 保存的原始位置。要使光标返回到其上次保存的位置,请包括 rc 选项或“restore cursor position”。
注意:由于本文首先详细介绍了通过命令行执行 tput,因此您可能会觉得在自己的 subshell 中执行命令要比单独执行每条命令然后在每条命令执行之前显示提示更简洁。
05. 文本属性
更改文本的显示方式可以让用户注意到菜单中的一组词或警惕用户注意某些重要的内容。您可以通过以下方式更改文本属性:使文本加粗、在文本下方添加下划线、更改背景颜色和前景颜色,以及逆转颜色方案等。
要更改文本的颜色,请使用 setb 选项(用于设置背景颜色)和 setf 选项(用于设置前景颜色)以及在 terminfo 数据库中分配的颜色数值。通常情况下,分配的数值与颜色的对应关系如下,但是可能会因 UNIX 系统的不同而异:
- 0:黑色
- 1:蓝色
- 2:绿色
- 3:青色
- 4:红色
- 5:洋红色
- 6:黄色
- 7:白色
执行以下示例命令可以将背景颜色更改为黄色,将前景颜色更改为红色:
[deng@localhost ~]$ tput setb 6
[deng@localhost ~]$ tput setf 4
[deng@localhost ~]$
- 1
- 2
- 3
反显当前的颜色方案
[deng@localhost ~]$ tput rev
[deng@localhost ~]$
- 1
- 2
要将文本更改为粗体,请使用 bold 选项。要开始添加下划线,请使用 smul 选项。在完成显示带下划线的文本后,请使用 rmul 选项。
评论记录:
回复评论: