shell 終端terminfo命令 tput

tput命令

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

image

文本屬性

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                             # 退出時清理終端,恢復光標顯示

效果如圖終端

image

相關文章
相關標籤/搜索