当前位置: 首页 > news >正文

linux命令之tput

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

http://www.lryc.cn/news/328760.html

相关文章:

  • python基础——文件操作【文件编码、文件的打开与关闭操作、文件读写操作】
  • rustup update 升级rust时异常 directory does not exist: ‘share/doc/rust/html‘ 解决方法
  • 算法学习——LeetCode力扣动态规划篇5
  • C语言-文件
  • 牛客NC30 缺失的第一个正整数【simple map Java,Go,PHP】
  • Unity 基于Rigidbody2D模块的角色移动
  • Stata 15 for Mac:数据统计分析新标杆,让研究更高效!
  • vue配置代理proxy
  • .NET DES加密算法实现
  • 构建操作可靠的数据流系统
  • awesome-cheatsheets:超级速查表 - 编程语言、框架和开发工具的速查表
  • GFW不起作用
  • AndroidStudio出现类似 Could not create task ‘:app:ToolOperatorDemo.main()‘. 错误
  • 一些常见的ClickHouse问题和答案
  • 第九届蓝桥杯大赛个人赛省赛(软件类)真题C 语言 A 组-分数
  • 并发编程——4.线程池
  • 阿里云魔搭发起“ModelScope-Sora开源计划”,将为中国类Sora模型开发提供一站式工具链
  • 大模型与数据分析:探索Text-to-SQL
  • Unity VisionOS开发流程
  • 聊聊k8s服务发现的优缺点
  • Tomcat是如何处理并发请求的?
  • H12-831_561
  • Java23种常见设计模式汇总
  • 神经网络与深度学习(一)
  • 算法学习——LeetCode力扣图论篇2
  • 大数据设计为何要分层,行业常规设计会有几层数据
  • css3之2D转换transform
  • pytest中文使用文档----6临时目录和文件
  • 从0开始搭建基于VUE的前端项目
  • elementUI this.$msgbox msgBox自定义 样式自定义 富文本