Linux shell编程学习笔记27:tputs

除了stty命令,我们还可以使用tput命令来更改终端的参数和功能。

1 tput 命令的功能

tput 命令的主要功能有:移动更改光标、更改文本显示属性(如颜色、下划线、粗体),清除屏幕特定区域等。

2 tput 命令格式

tput [选项] [参数]

3 设置文本颜色属性

3.1 tput setaf / setbf :设置前景色 / 背景色

命令格式:

  • tput setab n : 设置背景色,set text attributes background color
  • tput setaf n : 设置前景色,set text attributes front color

​参数n:

  • 0 -- Black,黑色
  • 1 -- Red,红色
  • 2 -- Green,绿色
  • 3 -- Yellow,黄色
  • 4 -- Blue,蓝色
  • 5 -- Magenta,品红
  • 6 -- Cyan,青色
  • 7 -- White,白色

例子:设置前背景色为黄色(3),背景色为品红(5)。

csdn @ edu bash ~ ++tput setaf 3 setab 5++ csdn @ edu bash \~ ++tput setab 5++
csdn @ edu bash ~ $

看来不能在一个命令中同时设置前景色和背景色,要分开来用两个命令分别设置。

csdn @ edu bash ~ tput setaf 3; tput setab 5 csdn @ edu bash \~

3.2 tput rev:反显当前的颜色方案

rev源自reverse。

csdn @ edu bash ~ ++tput setaf 3; tput setab 5++ csdn @ edu bash \~ ++tput rev++
csdn @ edu bash ~ $

4 设置文本模式属性

|-----------|-------------------------------------------|
| 命令 | 功能 |
| tput bold | 加粗字体 |
| tput dim | 打开高亮模式,turn on half-bright mode |
| tput smul | 添加下划线,start mode of underline, |
| tput rmul | 取消下划线,remove mode of underline |
| tput smso | 突出模式,start mode of standout |
| tput rmso | 反显突出模式,reverse mode of standout |
| tput sgr0 | set global attributes return to 0, 取消所有属性 |

csdn @ edu bash ~ $ ++tput setaf 3; tput setab 5++

csdn @ edu bash ~ $ ++tput smso++

csdn @ edu bash ~ $ ++tput rmso++

csdn @ edu bash ~ $ ++tput smul++

csdn @ edu bash ~ $ ++tput bold++

csdn @ edu bash ~ $ ++tput rmul++

csdn @ edu bash ~ $ ++tput dim++

csdn @ edu bash ~ $ ++tput sgr0++

csdn @ edu bash ~ $

5 设置光标属性

|---------------------|-------------------------------|
| 命令 | 功能 |
| tput clear | 清屏 |
| tput sc | 保存当前光标位置,save curosr position |
| tput rc | 恢复光标位置,restore cursor postion |
| tput cup row column | 将光标移动到指定行row和列column, |
| tput civis | 光标不可见,cursor invisible |
| tput cnorm | 光标可见,cursor mormal |
| tpu init | 清空格式 |

我们将执行以下命令序列:

bash 复制代码
tput clear                   # 清屏
tput sc                      # 保存当前光标位置
tput cup 10 13               # 将光标移动到第10行第13列
echo -n Enter your password: # 提示用户输入密码
tput civis                   # 光标不可见
read p                       # 将用户输入的密码保存到变量p中
tput cnorm                   # 光标可见
tput rc                      # 恢复光标位置
echo your password: $p       # 显示用户输入的密码

csdn @ edu bash ~ ++tput clear;tput sc;tput cup 10 13;echo -n Enter your password:;tput civis; read p; tput cnorm; tput rc; echo your password: p++

6 其它功能

|---------------|--------------|
| 命令 | 功能 |
| tput lines | 显示行数 |
| tpus cols | 显示列数 |
| tput reset | 重置终端设置 |
| tput longname | 显示当前终端类型的长名称 |
| tput hs | 具有状态行 |

csdn @ edu bash ~ $ ++tput lines++

22

csdn @ edu bash ~ $ ++tput cols++

132

csdn @ edu bash ~ $ ++tput longname++

xterm terminal emulator (X Window System)csdn @ edu bash ~ $

相关推荐
yangshuo12811 小时前
风车OVF镜像:解放AI开发限制的Ubuntu精简系统
linux·人工智能·ubuntu
flypig哗啦啦1 小时前
ubuntu服务器版启动卡在start job is running for wait for...to be Configured
linux·运维·ubuntu
_Itachi__1 小时前
LeetCode 热题 100 114. 二叉树展开为链表
linux·leetcode·链表
あ-1 小时前
CentOS7 OpenSSL升级1.1.1w
linux
tianyuanwo2 小时前
深入解读tcpdump:原理、数据结构与操作手册
linux·运维·服务器·tcpdump
电鱼智能的电小鱼3 小时前
产线视觉检测设备技术方案:基于EFISH-SCB-RK3588/SAIL-RK3588的国产化替代赛扬N100/N150全场景技术解析
linux·人工智能·嵌入式硬件·计算机视觉·视觉检测·实时音视频
子非衣3 小时前
Ubuntu系统挂载磁盘并配置开机自动挂载
linux·运维·ubuntu
风暴智能4 小时前
问题处理——在ROS2(humble)+Gazebo+rqt下,无法显示仿真无人机的相机图像
linux·无人机
唐志远19974 小时前
VMware虚拟机 安装 CentOS 7
linux·运维·centos
众乐乐_20084 小时前
Uniapp中小程序调用腾讯地图(获取定位地址)
linux·服务器·apache