linux命令之tput

本文详细介绍了Linux终端中的tput命令,包括清除屏幕、设置文本样式、获取终端尺寸、光标定位、隐藏/显示光标、以及文本颜色设置,并通过示例和bash脚本展示了其实际应用。
摘要由CSDN通过智能技术生成

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/bash

blue_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/bash

blue_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 ~]# 

  • 24
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑要上天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值