tput 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

tput 命令安装:


  1. -bash: tput: command not found
  2.  
  3. #Debian
  4. apt-get install libncurses5-dbg
  5.  
  6. #Ubuntu
  7. apt-get install libncurses5-dbg
  8.  
  9. #Alpine
  10. apk add ncurses
  11.  
  12. #Arch Linux
  13. pacman -S ncurses
  14.  
  15. #Kali Linux
  16. apt-get install ncurses-bin
  17.  
  18. #CentOS
  19. yum install ncurses
  20.  
  21. #Fedora
  22. dnf install ncurses
  23.  
  24. #OS X
  25. brew install ncurses
  26. #Raspbian
  27. apt-get install libncurses5-dbg
  28.  
  29. #Docker
  30. docker run cmd.cat/tput tput

tput 命令语法:


  1. tput [-Ttype] capname [params ... ]
  2. tput [-Ttype] init
  3. tput [-Ttype] reset
  4. tput [-Ttype] longname
  5. tput -S <<
  6. tput -V

tput 命令补充说明:


tput 命令将通过 terminfo 数据库对您的终端会话进行初始化和操作。通过使用 tput,您可以更改几项终端功能,如移动或更改光标、更改文本属性,以及清除终端屏幕的特定区域

tput 命令光标属性:


  1. tput clear # 清除屏幕
  2. tput sc # 记录当前光标位置
  3. tput rc # 恢复光标到最后保存位置
  4. tput civis # 光标不可见
  5. tput cnorm # 光标可见
  6. tput cup x y # 光标按设定坐标点移动

tput 命令文本属性:


  1. tput blink # 文本闪烁
  2. tput bold # 文本加粗
  3. tput el # 清除到行尾
  4. tput smso # 启动突出模式
  5. tput rmso # 停止突出模式
  6. tput smul # 下划线模式
  7. tput rmul # 取消下划线模式
  8. tput sgr0 # 恢复默认终端
  9. tput rev # 反相终端

tput 命令知识扩展:


更改光标的属性

在向某一设备显示数据时,很多时候您并不希望看到光标。将光标转换为不可见可以使数据滚动时的屏幕看起来更整洁。要使光标不可见,请使用 civis 选项(例如,tput civis)。在数据完全显示之后,您可以使用 cnorm 选项将光标再次转变为可见。

文本属性

更改文本的显示方式可以让用户注意到菜单中的一组词或警惕用户注意某些重要的内容。您可以通过以下方式更改文本属性:使文本加粗、在文本下方添加下划线、更改背景颜色和前景颜色,以及逆转颜色方案等。

要更改文本的颜色,请使用 setb 选项(用于设置背景颜色)和 setf 选项(用于设置前景颜色)以及在 terminfo 数据库中分配的颜色数值。通常情况下,分配的数值与颜色的对应关系如下,但是可能会因 UNIX 系统的不同而异:

  1. 0:黑色
  2. 1:蓝色
  3. 2:绿色
  4. 3:青色
  5. 4:红色
  6. 5:洋红色
  7. 6:黄色
  8. 7:白色

执行以下示例命令可以将背景颜色更改为黄色,将前景颜色更改为红色:

  1. tput setb 6 tput setf 4

要反显当前的颜色方案,只需执行tput rev。

有时,仅为文本着色还不够,也就是说,您想要通过另一种方式引起用户的注意。可以通过两种方式达到这一目的:一是将文本设置为粗体,二是为文本添加下划线

要将文本更改为粗体,请使用 bold 选项。要开始添加下划线,请使用 smul 选项。在完成显示带下划线的文本后,请使用 rmul 选项

tput 命令实例:


要清除当前终端的屏幕,请输入:

  1. tput clear

要显示当前终端的列数,请输入:

  1. tput cols

要显示 aixterm 终端的列数,请输入:

  1. tput -T aixterm cols

要将 shell 变量 bold 设置为开始突出方式顺序并将 shell 变量 offbold 设置为结束突出方式顺序:

  1. bold=`tput smso` offbold='tput rmso'

输入这些指令后,可能会出现以下提示符:

  1. echo "${bold}Name: ${offbold} \c"

要设置出口值以指示当前终端是否是一个硬拷贝终端,请输入:

  1. tput hc

要对当前终端进行初始化,请输入:

  1. tput init

使输出的字符串有颜色,底色,加粗

提示:将下面复制写在诸如 test.sh,并添加权限 chmod u+x test.sh,之后运行 ./test.sh 查看效果):

  1. #!/bin/bash
  2. printf $(tput setaf 2; tput bold)'color show\n\n'$(tput sgr0)
  3.  
  4. for((i=0; i<=7; i++)); do
  5. echo $(tput setaf $i)"show me the money"$(tput sgr0)
  6. done
  7.  
  8. printf '\n'$(tput setaf 2; tput setab 0; tput bold)'background color show'$(tput sgr0)'\n\n'
  9.  
  10. for((i=0,j=7; i<=7; i++,j--)); do
  11. echo $(tput setaf $i; tput setab $j; tput bold)"show me the money"$(tput sgr0)
  12. done
  13.  
  14. exit 0

输出格式控制函数:

  1. #!/bin/bash
  2.  
  3. # $1 str print string
  4. # $2 color 0-7 设置颜色
  5. # $3 bgcolor 0-7 设置背景颜色
  6. # $4 bold 0-1 设置粗体
  7. # $5 underline 0-1 设置下划线
  8.  
  9. function format_output(){
  10. str=$1
  11. color=$2
  12. bgcolor=$3
  13. bold=$4
  14. underline=$5
  15. normal=$(tput sgr0)
  16.  
  17. case "$color" in
  18. 0|1|2|3|4|5|6|7)
  19. setcolor=$(tput setaf $color;) ;;
  20. *)
  21. setcolor="" ;;
  22. esac
  23.  
  24. case "$bgcolor" in
  25. 0|1|2|3|4|5|6|7)
  26. setbgcolor=$(tput setab $bgcolor;) ;;
  27. *)
  28. setbgcolor="" ;;
  29. esac
  30.  
  31. if [ "$bold" = "1" ]; then
  32. setbold=$(tput bold;)
  33. else
  34. setbold=""
  35. fi
  36.  
  37. if [ "$underline" = "1" ]; then
  38. setunderline=$(tput smul;)
  39. else
  40. setunderline=""
  41. fi
  42.  
  43. printf "$setcolor$setbgcolor$setbold$setunderline$str$normal\n"
  44. }
  45.  
  46. format_output "Yesterday Once more" 2 5 1 1
  47.  
  48. exit 0

光标属性例子:

  1. #!/bin/bash
  2. # clear the screen
  3. tput clear
  4. # Move cursor to screen location X,Y (top left is 0,0)
  5. tput cup 3 15
  6. # set a foreground colour using ANSI escape
  7. tput setaf 3
  8. echo "XYX Corp LTD."
  9. tput sgr0
  10. tput cup 5 17
  11. # Set reverse video mode
  12. tput rev
  13. echo "M A I N - M E N U"
  14. tput sgr0
  15. tput cup 7 15
  16. echo "1\. User Management"
  17. tput cup 8 15
  18. echo "2\. service Management"
  19. tput cup 9 15
  20. echo "3\. Process Management"
  21. tput cup 10 15
  22. echo "4\. Backup"
  23. # Set bold mode
  24. tput bold
  25. tput cup 12 15
  26. read -p "Enter your choice [1-4] " choice
  27. tput clear
  28. tput sgr0
  29. tput rc
  30.  
  31. exit 0
  32.  
  33. ################################## 运行效果如下:##################################
  34.  
  35.  
  36.  
  37. XYX Corp LTD.
  38.  
  39. M A I N - M E N U
  40.  
  41. 1\. User Management
  42. 2\. service Management
  43. 3\. Process Management
  44. 4\. Backup
  45.  
  46. Enter your choice [1-4] 1
  47.  

利用上面光标属性参数编写一个终端时钟

  1. #!/bin/bash
  2.  
  3. for ((i=0;i<10;i++))
  4. do
  5. tput sc; tput civis # 记录光标位置,及隐藏光标
  6. echo -ne $(date +'%Y-%m-%d %H:%M:%S') # 显示时间
  7. sleep 1
  8. tput rc # 恢复光标到记录位置
  9. done
  10.  
  11. tput el; tput cnorm # 退出时清理终端,恢复光标显示

现在为"终端时钟"添加,变换颜色和闪烁功能

  1. #!/bin/bash
  2.  
  3. for ((i=0;i<8;i++))
  4. do
  5. tput sc; tput civis # 记录光标位置,及隐藏光标
  6. tput blink; tput setf $i # 文本闪烁,更改文本颜色
  7. echo -ne $(date +'%Y-%m-%d %H:%M:%S') # 显示时间
  8. sleep 1
  9. tput rc # 恢复光标到记录位置
  10. done
  11.  
  12. tput el; tput cnorm # 退出时清理终端,恢复光标显示

tput 命令扩展阅读:




tput 命令评论