tput
能够更改终端功能,如移动或更改光标,更改文本属性,清除终端屏幕的特定区域等。shell
在shell脚本或命令行中,能够利用tput命令改变光标属性。bash
tput clear # 清除屏幕 tput sc # 记录当前光标位置 tput rc # 恢复光标到最后保存位置 tput civis # 光标不可见 tput cnorm # 光标可见 tput cup x y # 光标按设定坐标点移动
利用上面参数编写一个终端时钟命令行
#!/bin/bash for ((i=0;i<10;i++)) do tput sc; tput civis # 记录光标位置,及隐藏光标 echo -ne $(date +'%Y-%m-%d %H:%M:%S') # 显示时间 sleep 1 tput rc # 恢复光标到记录位置 done tput el; tput cnorm # 退出时清理终端,恢复光标显示
效果如图code
tput
可以使终端文本加粗、在文本下方添加下划线、更改背景颜色和前景颜色,以及逆转颜色方案等。orm
tput blink # 文本闪烁 tput bold # 文本加粗 tput el # 清除到行尾 tput smso # 启动突出模式 tput rmso # 中止突出模式 tput smul # 下划线模式 tput rmul # 取消下划线模式 tput sgr0 # 恢复默认终端 tput rev # 反相终端
此外,还能够改变文本的颜色ci
tput setb 颜色代号 tput setf 颜色代号
颜色代号为class
0:黑色 1:蓝色 2:绿色 3:青色 4:红色 5:洋红色 6:黄色 7:白色
如今为"终端时钟"添加,变换颜色和闪烁功能date
#!/bin/bash for ((i=0;i<8;i++)) do tput sc; tput civis # 记录光标位置,及隐藏光标 tput blink; tput setf $i # 文本闪烁,更改文本颜色 echo -ne $(date +'%Y-%m-%d %H:%M:%S') # 显示时间 sleep 1 tput rc # 恢复光标到记录位置 done tput el; tput cnorm # 退出时清理终端,恢复光标显示
效果如图终端