概念

  • tput
    tput 命令将通过 terminfo 数据库对终端会话进行初始化和操作。
  • terminfo?
    UNIX 系统上的 terminfo 数据库用于定义终端和打印机的属性及功能,包括各设备(例如,终端和打印机)的行数和列数以及要发送至该设备的文本的属性。UNIX 中的几个常用程序都依赖 terminfo 数据库提供这些属性以及许多其他内容,其中包括 vi 和 emacs 编辑器以及 curses 和 man 程序。

tput常用参数即使用功能键

常用参数:

参数用途分类 参数名 参数含义
字符串输出
bel 警铃
blink 闪烁模式
bold 粗体
civis 隐藏光标
clear 清屏
cnorm 不隐藏光标
cup 移动光标到屏幕位置(x,y)
el 清除到行尾
el1 清除到行首(注意结尾是数字一不是字母l)
smso 启动突出模式
rmso 停止突出模式
smul 开始下划线模式
rmul 结束下划线模式
sc 保存当前光标位置
rc 恢复光标到最后保存位置
sgr0 正常屏幕
rev 逆转视图
数字输出
cols 列数目
ittab 设置宽度
lines 屏幕行数
布尔输出
chts 光标不可见
hs 具有状态行
tput setb no 设置终端背景色。no的取值:0:黑色、1:蓝色、2:绿色、3:青色、4:红色、5:洋红色、6:黄色、7:白色
tput setf no 设置文本的颜色。no的取值:0:黑色、1:蓝色、2:绿色、3:青色、4:红色、5:洋红色、6:黄色、7:白色
tput ed 删除当前光标到行尾的内容

可以使用infocmp命令检查终端信息(通过制定名称或者不指定名称的方式)

infocmp $TERM  
infocmp

如:

[root@mesoscloud_test255148 yueling]# infocmp 
#	Reconstructed via infocmp from file: /usr/share/terminfo/x/xterm
xterm|xterm terminal emulator (X Window System),am, bce, km, mc5i, mir, msgr, npc, xenl,colors#8, cols#80, it#8, lines#24, pairs#64,acsc=``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,bel=^G, blink=\E[5m, bold=\E[1m, cbt=\E[Z, civis=\E[?25l,clear=\E[H\E[2J, cnorm=\E[?12l\E[?25h, cr=^M,csr=\E[%i%p1%d;%p2%dr, cub=\E[%p1%dD, cub1=^H,cud=\E[%p1%dB, cud1=^J, cuf=\E[%p1%dC, cuf1=\E[C,cup=\E[%i%p1%d;%p2%dH, cuu=\E[%p1%dA, cuu1=\E[A,cvvis=\E[?12;25h, dch=\E[%p1%dP, dch1=\E[P, dl=\E[%p1%dM,dl1=\E[M, ech=\E[%p1%dX, ed=\E[J, el=\E[K, el1=\E[1K,flash=\E[?5h$<100/>\E[?5l, home=\E[H, hpa=\E[%i%p1%dG,ht=^I, hts=\EH, ich=\E[%p1%d@, il=\E[%p1%dL, il1=\E[L,ind=^J, indn=\E[%p1%dS, invis=\E[8m,is2=\E[!p\E[?3;4l\E[4l\E>, kDC=\E[3;2~, kEND=\E[1;2F,kHOM=\E[1;2H, kIC=\E[2;2~, kLFT=\E[1;2D, kNXT=\E[6;2~,kPRV=\E[5;2~, kRIT=\E[1;2C, kb2=\EOE, kbs=\177, kcbt=\E[Z,kcub1=\EOD, kcud1=\EOB, kcuf1=\EOC, kcuu1=\EOA,kdch1=\E[3~, kend=\EOF, kent=\EOM, kf1=\EOP, kf10=\E[21~,kf11=\E[23~, kf12=\E[24~, kf13=\E[1;2P, kf14=\E[1;2Q,kf15=\E[1;2R, kf16=\E[1;2S, kf17=\E[15;2~, kf18=\E[17;2~,kf19=\E[18;2~, kf2=\EOQ, kf20=\E[19;2~, kf21=\E[20;2~,kf22=\E[21;2~, kf23=\E[23;2~, kf24=\E[24;2~,kf25=\E[1;5P, kf26=\E[1;5Q, kf27=\E[1;5R, kf28=\E[1;5S,kf29=\E[15;5~, kf3=\EOR, kf30=\E[17;5~, kf31=\E[18;5~,kf32=\E[19;5~, kf33=\E[20;5~, kf34=\E[21;5~,kf35=\E[23;5~, kf36=\E[24;5~, kf37=\E[1;6P, kf38=\E[1;6Q,kf39=\E[1;6R, kf4=\EOS, kf40=\E[1;6S, kf41=\E[15;6~,kf42=\E[17;6~, kf43=\E[18;6~, kf44=\E[19;6~,kf45=\E[20;6~, kf46=\E[21;6~, kf47=\E[23;6~,kf48=\E[24;6~, kf49=\E[1;3P, kf5=\E[15~, kf50=\E[1;3Q,kf51=\E[1;3R, kf52=\E[1;3S, kf53=\E[15;3~, kf54=\E[17;3~,kf55=\E[18;3~, kf56=\E[19;3~, kf57=\E[20;3~,kf58=\E[21;3~, kf59=\E[23;3~, kf6=\E[17~, kf60=\E[24;3~,kf61=\E[1;4P, kf62=\E[1;4Q, kf63=\E[1;4R, kf7=\E[18~,kf8=\E[19~, kf9=\E[20~, khome=\EOH, kich1=\E[2~,kind=\E[1;2B, kmous=\E[M, knp=\E[6~, kpp=\E[5~,kri=\E[1;2A, mc0=\E[i, mc4=\E[4i, mc5=\E[5i, meml=\El,memu=\Em, op=\E[39;49m, rc=\E8, rev=\E[7m, ri=\EM,rin=\E[%p1%dT, rmacs=\E(B, rmam=\E[?7l, rmcup=\E[?1049l,rmir=\E[4l, rmkx=\E[?1l\E>, rmm=\E[?1034l, rmso=\E[27m,rmul=\E[24m, rs1=\Ec, rs2=\E[!p\E[?3;4l\E[4l\E>, sc=\E7,setab=\E[4%p1%dm, setaf=\E[3%p1%dm,setb=\E[4%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m,setf=\E[3%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}%=%t3%e%p1%d%;m,sgr=%?%p9%t\E(0%e\E(B%;\E[0%?%p6%t;1%;%?%p2%t;4%;%?%p1%p3%|%t;7%;%?%p4%t;5%;%?%p7%t;8%;m,sgr0=\E(B\E[m, smacs=\E(0, smam=\E[?7h, smcup=\E[?1049h,smir=\E[4h, smkx=\E[?1h\E=, smm=\E[?1034h, smso=\E[7m,smul=\E[4m, tbc=\E[3g, u6=\E[%i%d;%dR, u7=\E[6n,u8=\E[?1;2c, u9=\E[c, vpa=\E[%i%p1%dd,
[root@mesoscloud_test255148 yueling]#

如何输入功能键:
功能键通常不能正常输入,输入方式可以是由控制台检验:ctrl+v 再按功能键

F1:在控制台按下ctrl+v 再按下F1
[root@mesoscloud_test255148 yueling]# ^[OP
#!/bin/bash
upkey='^[[A'
f1key='^[OP'while true
doread  -p "press key:" keycase $key in$upkey) echo "up key";;$f1key) echo "F1 key";;*) echo "other key";;esac
done

demo

针对常用参数举一些简单例子

[root@mesoscloud_test255148 yueling]#cat tputTest.sh
tput clear
echo "触发系统蜂鸣"
tput bel
sleep 1echo "闪烁模式"
tput blink
echo -e "\t闪烁模式测试内容"
sleep 1
tput sgr0
echo "恢复正常屏幕"echo "粗体"
tput bold
echo -e "\t粗体测试内容"
sleep 1
tput sgr0
echo "恢复正常屏幕"echo "隐藏光标"
tput civis
echo -e "\t隐藏光标测试内容"
sleep 1
tput cnorm
echo "恢复正常屏幕"echo "移动光标"
tput cup 10 10
echo -e "\t移动光标测试内容"
sleep 1
tput sgr0
echo "恢复正常屏幕"echo "清屏"
echo -e "\t清屏测试"
sleep 1
tput clearecho "xxxxxxxxxxUxxxxxxxxxx"
tput cup 0 9
sleep 1
tput el1tput cup 1 0
echo "xxxxxxxxxxUxxxxxxxxxx"
tput cup 1 9
sleep 1
tput eltput cup 2 0
echo "突出模式启用"
tput smso
echo -e "\t突出模式启用测试"
sleep 1
tput rmso
echo "停止突出模式测试内容"echo "下划线模式启用"
tput smul
echo -e "\t下划线模式启用测试"
sleep 1
tput rmul
echo "结束下划线模式测试"echo "逆转视图"
tput rev
echo -e "\t逆转视图测试"
sleep 1
tput sgr0
echo "恢复正常屏幕"echo "输出列"
tput cols
echo "输出行"
tput lines
echo "设置背景颜色"
tput setb 1
echo -e "\t测试带背景颜色"
echo "设置前景颜色"
tput setf 4
echo -e "\t测试带前景颜色"
tput sgr0
echo "恢复正常屏幕"

#综合demo
光标属性综合示例
例如显示系统时钟

#!/bin/bash#tput clear      # 清除屏幕
#tput sc         # 记录当前光标位置
#tput rc         # 恢复光标到最后保存位置
#tput civis      # 光标不可见
#tput cnorm      # 光标可见
#tput cup x y    # 光标按设定坐标点移动while true
dotput sc;                                # 记录光标位置echo -ne $(date +'%Y-%m-%d %H:%M:%S')   # 显示时间sleep 1tput rc                                 # 恢复光标到记录位置,不恢复会一直累计显示
done

文本属性示例

#!/bin/bash#tput blink      # 文本闪烁
#tput bold       # 文本加粗
#tput el         # 清除到行尾
#tput smso       # 启动突出模式
#tput rmso       # 停止突出模式
#tput smul       # 下划线模式
#tput rmul       # 取消下划线模式
#tput sgr0       # 恢复默认终端
#tput rev        # 反相终端count=0;
tput sgr0
while true
dotput sc;                                       # 记录光标位置tput setf $((count%7))                         #更改文本颜色if [ $((count%2==0)) -eq 0 ]thentput smulelsetput rmulfifor fg in {0..7}; doset_fg=$(tput setf $fg)for bg in {0..7}; doset_bg=$(tput setb $bg)echo -ne $set_bg$set_fgecho -ne $(date +'%Y-%m-%d %H:%M:%S')doneecho donesleep 1tput rc                                       # 恢复光标到记录位置((count++))
done

以上两个例子均使用的死循环,强制退出时会改变常规显示,使用tput sgr0 恢复即可

编写脚本常用的模版参考

[root@mesoscloud_test255148 yueling]# cat menuTemplate.sh 
#!/bin/bashb_blue="$(tput setab 4)"
b_black="$(tput setab 0)"
f_green="$(tput setaf 2)"
f_white="$(tput setaf 7)"# Save screen
tput smcupchild() {while [[ $REPLY != q ]];dotput cup 13 0cat <<- _EOF_请选择操作:1. child.12. child.2q. Quit_EOF_read -p "请选择 [0-2|q] > "tput cup 19 0echo -n ${b_black}${f_green}tput edtput cup 21 0case ${REPLY} in[1-2])  _X=`expr ${REPLY} - 1`; ls /root ;echo ${_X};;q)  break;;*)  echo "非法参数"; continue;;esacdone
}# Display menu until selection == 0
while [[ $REPLY != q ]]; doecho -n ${b_blue}${f_white}clearcat <<- _EOF_请选择操作:1. No.12. No.23. No.34. hasChildq. Quit_EOF_read -p "请选择 [0-3|q] > " selection# Clear area beneath menutput cup 10 0echo -n ${b_black}${f_green}tput edtput cup 11 0case $selection in1)  echo "No.1" ;;2)  echo "No.2";;3)  echo "No.3";;4)  child;;q)  break;;*)  echo "非法参数";;esacprintf "\n\n任意键继续"read -n 1
done# Restore screen
tput rmcup

一个简单的架子