1.tput介绍
linux命令tput是可以在终端中进行文本和颜色的控制和格式化,其是一个非常有用的命令
2.tput用法
命令:
man tput
3.样例
3.1.清除屏幕
命令:
tput clear
[root@elasticsearch ~]# tput clear
[root@elasticsearch ~]#
3.2.设置文本样式
加粗:
命令:
tput bold
[root@elasticsearch ~]# tput bold #加粗
[root@elasticsearch ~]# echo "This is bold style."
This is bold style.
[root@elasticsearch ~]# tput sgr0 #恢复默认样式
[root@elasticsearch ~]#
3.3.获取终端的行数和列数
命令:
lines=$(tput lines)
columns=$(tput cols)
echo "Terminal has $lines lines and $columns columns."
[root@elasticsearch ~]# lines=$(tput lines)
[root@elasticsearch ~]# columns=$(tput cols)
[root@elasticsearch ~]# echo "Terminal has $lines lines and $columns columns."
Terminal has 10 lines and 113 columns.
[root@elasticsearch ~]#
3.4.移动光标的位置
命令:
tput cup 2 20
#将光标移动到第2行20列
[root@elasticsearch ~]# tput cup 2 20[root@elasticsearch ~]#
3.5.隐藏光标
命令:
tput civis
[root@elasticsearch ~]# tput civis
[root@elasticsearch ~]#
[root@elasticsearch ~]#
3.6.显示光标
命令:
tput cnorm
[root@elasticsearch ~]# tput cnorm
[root@elasticsearch ~]#
[root@elasticsearch ~]#
3.7.设置文本颜色
tput命令可以用来设置文本的前景和背景颜色
3.7.1.将文本字体颜色设置为绿色
命令:
tput setaf 2
echo "This is green text."
tput sgr0
[root@elasticsearch ~]# tput setaf 2
[root@elasticsearch ~]# echo "This is green text."
This is green text.
[root@elasticsearch ~]# tput sgr0
[root@elasticsearch ~]#
3.7.2.将文本背景颜色设置为红色
命令:
tput setab 1
echo "This text has a red background."
tput sgr0
[root@elasticsearch ~]# tput setab 1
[root@elasticsearch ~]# echo "This text has a red background."
This text has a red background.
[root@elasticsearch ~]# tput sgr0
[root@elasticsearch ~]#
通常情况下,文本字体颜色及文本背景颜色分配的数值与颜色的对应关系如下(可能会因 UNIX 系统的不同而异):
数值 | 颜色 |
0 | 黑色 |
1 | 红色 |
2 | 绿色 |
3 | 黄色 |
4 | 蓝色 |
5 | 紫色 |
6 | 深绿色 |
7 | 灰色 |
3.7.3.文本综合案例
设置文本字体颜色为蓝色,文本背景颜色为灰色
脚本:
#!/bin/bashblue_font=$(tput setaf 4)
grey_background=$(tput setab 7)
reset_normal=$(tput sgr0)#
echo "${blue_font}${grey_background}I am ztj.${reset_normal}"
echo "${blue_font}${grey_background}I work in the Bayshore Consulting & Services Co.,Ltd.${reset_normal}"
echo "${blue_font}${grey_background}I am from XX,China Mainland .${reset_normal}"exit 0
[root@elasticsearch ~]# cat tput.sh
#!/bin/bashblue_font=$(tput setaf 4)
grey_background=$(tput setab 7)
reset_normal=$(tput sgr0)#
echo "${blue_font}${grey_background}I am ztj.${reset_normal}"
echo "${blue_font}${grey_background}I work in the Bayshore Consulting & Services Co.,Ltd.${reset_normal}"
echo "${blue_font}${grey_background}I am from XX,China Mainland .${reset_normal}"exit 0
[root@elasticsearch ~]# sh tput.sh
I am ztj.
I work in the Bayshore Consulting & Services Co.,Ltd.
I am from XX,China Mainland .
[root@elasticsearch ~]#