轉載自:https://blog.csdn.net/fdipzone/article/details/9993961數據庫
什麼是 tput?
tput 命令將經過 terminfo 數據庫對您的終端會話進行初始化和操做。經過使用 tput,您能夠更改幾項終端功能,如移動或更改光標、更改文本屬性,以及清除終端屏幕的特定區域。bash
什麼是 terminfo 數據庫?
UNIX 系統上的 terminfo 數據庫用於定義終端和打印機的屬性及功能,包括各設備(例如,終端和打印機)的行數和列數以及要發送至該設備的文本的屬性。UNIX 中的幾個經常使用程序都依賴 terminfo 數據庫提供這些屬性以及許多其餘內容,其中包括 vi 和 emacs 編輯器以及 curses 和 man 程序。編輯器
命令行使用說明:ide
1.文本屬性函數
tput Color Capabilities:.net
tput setab [0-7] – Set a background color using ANSI escape
tput setb [0-7] – Set a background color
tput setaf [0-7] – Set a foreground color using ANSI escape
tput setf [0-7] – Set a foreground color命令行
Color Code for tput:orm
0 – Black
1 – Red
2 – Green
3 – Yellow
4 – Blue
5 – Magenta
6 – Cyan
7 – Whiteblog
tput Text Mode Capabilities:ip
tput bold – Set bold mode
tput dim – turn on half-bright mode
tput smul – begin underline mode
tput rmul – exit underline mode
tput rev – Turn on reverse mode
tput smso – Enter standout mode (bold on rxvt)
tput rmso – Exit standout mode
tput sgr0 – Turn off all attributes
例子:使輸出的字符串有顏色,底色,加粗
#!/bin/bash printf $(tput setaf 2; tput bold)'color show\n\n'$(tput sgr0) for((i=0; i<=7; i++)); do echo $(tput setaf $i)"show me the money"$(tput sgr0) done printf '\n'$(tput setaf 2; tput setab 0; tput bold)'background color show'$(tput sgr0)'\n\n' for((i=0,j=7; i<=7; i++,j--)); do echo $(tput setaf $i; tput setab $j; tput bold)"show me the money"$(tput sgr0) done exit 0
輸出格式控制函數:
#!/bin/bash # $1 str print string # $2 color 0-7 設置顏色 # $3 bgcolor 0-7 設置背景顏色 # $4 bold 0-1 設置粗體 # $5 underline 0-1 設置下劃線 function format_output(){ str=$1 color=$2 bgcolor=$3 bold=$4 underline=$5 normal=$(tput sgr0) case "$color" in 0|1|2|3|4|5|6|7) setcolor=$(tput setaf $color;) ;; *) setcolor="" ;; esac case "$bgcolor" in 0|1|2|3|4|5|6|7) setbgcolor=$(tput setab $bgcolor;) ;; *) setbgcolor="" ;; esac if [ "$bold" = "1" ]; then setbold=$(tput bold;) else setbold="" fi if [ "$underline" = "1" ]; then setunderline=$(tput smul;) else setunderline="" fi printf "$setcolor$setbgcolor$setbold$setunderline$str$normal\n" } format_output "Yesterday Once More" 2 5 1 1 exit 0
2.光標屬性
#!/bin/bash
tput clear # 清屏
tput sc # 保存當前光標位置
tput cup 10 13 # 將光標移動到 row col
tput civis # 光標不可見
tput cnorm # 光標可見
tput rc # 顯示輸出
exit 0
例子:
#!/bin/bash # clear the screen tput clear # Move cursor to screen location X,Y (top left is 0,0) tput cup 3 15 # Set a foreground colour using ANSI escape tput setaf 3 echo "XYX Corp LTD." tput sgr0 tput cup 5 17 # Set reverse video mode tput rev echo "M A I N - M E N U" tput sgr0 tput cup 7 15 echo "1. User Management" tput cup 8 15 echo "2. Service Management" tput cup 9 15 echo "3. Process Management" tput cup 10 15 echo "4. Backup" # Set bold mode tput bold tput cup 12 15 read -p "Enter your choice [1-4] " choice tput clear tput sgr0 tput rc exit 0