黄色片网站免费观看-黄色片网站观看-黄色片网站大全-黄色片视频网-色偷偷网址-色偷偷网站

tput

通過terminfo數(shù)據(jù)庫對終端會話進(jìn)行初始化和操作

補充說明

tput命令 將通過 terminfo 數(shù)據(jù)庫對您的終端會話進(jìn)行初始化和操作。通過使用 tput,您可以更改幾項終端功能,如移動或更改光標(biāo)、更改文本屬性,以及清除終端屏幕的特定區(qū)域。

什么是 terminfo 數(shù)據(jù)庫?

UNIX 系統(tǒng)上的 terminfo 數(shù)據(jù)庫用于定義終端和打印機的屬性及功能,包括各設(shè)備(例如,終端和打印機)的行數(shù)和列數(shù)以及要發(fā)送至該設(shè)備的文本的屬性。UNIX 中的幾個常用程序都依賴 terminfo 數(shù)據(jù)庫提供這些屬性以及許多其他內(nèi)容,其中包括 vi 和 emacs 編輯器以及 curses 和 man 程序。

與 UNIX 中的大多數(shù)命令一樣,tput 命令既可以用在 shell 命令行中也可以用在 shell 腳本中。為讓您更好地理解 tput,本文首先從命令行講起,然后緊接著講述 shell 腳本示例。

光標(biāo)屬性

在 UNIX shell 腳本中或在命令行中,移動光標(biāo)或更改光標(biāo)屬性可能是非常有用的。有些情況下,您可能需要輸入敏感信息(如密碼),或在屏幕上兩個不同的區(qū)域輸入信息。在此類情況下,使用 tput 可能會對您有所幫助。

tput clear # 清屏
tput sc # 保存當(dāng)前光標(biāo)位置
tput cup 10 13 # 將光標(biāo)移動到 row col
tput civis # 光標(biāo)不可見
tput cnorm # 光標(biāo)可見
tput rc # 顯示輸出
exit 0

移動光標(biāo)

使用 tput 可以方便地實現(xiàn)在各設(shè)備上移動光標(biāo)的位置。通過在 tput 中使用 cup 選項,或光標(biāo)位置,您可以在設(shè)備的各行和各列中將光標(biāo)移動到任意 X 或 Y 坐標(biāo)。設(shè)備左上角的坐標(biāo)為 (0,0)。

要在設(shè)備上將光標(biāo)移動到第 5 列 (X) 的第 1 行 (Y),只需執(zhí)行 tput cup 5 1。另一個示例是 tput cup 23 45,此命令將使光標(biāo)移動到第 23 列上的第 45 行。

移動光標(biāo)并顯示信息

另一種有用的光標(biāo)定位技巧是移動光標(biāo),執(zhí)行用于顯示信息的命令,然后返回到前一光標(biāo)位置:

(tput sc ; tput cup 23 45 ; echo “Input from tput/echo at 23/45” ; tput rc)

下面我們分析一下 subshell 命令:

tput sc

必須首先保存當(dāng)前的光標(biāo)位置。要保存當(dāng)前的光標(biāo)位置,請包括 sc 選項或“save cursor position”。

tput cup 23 45

在保存了光標(biāo)位置后,光標(biāo)坐標(biāo)將移動到 (23,45)。

echo “Input from tput/echo at 23/45”

將信息顯示到 stdout 中。

tput rc

在顯示了這些信息之后,光標(biāo)必須返回到使用 tput sc 保存的原始位置。要使光標(biāo)返回到其上次保存的位置,請包括 rc 選項或“restore cursor position”。

注意:由于本文首先詳細(xì)介紹了通過命令行執(zhí)行 tput,因此您可能會覺得在自己的 subshell 中執(zhí)行命令要比單獨執(zhí)行每條命令然后在每條命令執(zhí)行之前顯示提示更簡潔。

更改光標(biāo)的屬性

在向某一設(shè)備顯示數(shù)據(jù)時,很多時候您并不希望看到光標(biāo)。將光標(biāo)轉(zhuǎn)換為不可見可以使數(shù)據(jù)滾動時的屏幕看起來更整潔。要使光標(biāo)不可見,請使用 civis 選項(例如,tput civis)。在數(shù)據(jù)完全顯示之后,您可以使用 cnorm 選項將光標(biāo)再次轉(zhuǎn)變?yōu)榭梢姟?/p>

文本屬性

更改文本的顯示方式可以讓用戶注意到菜單中的一組詞或警惕用戶注意某些重要的內(nèi)容。您可以通過以下方式更改文本屬性:使文本加粗、在文本下方添加下劃線、更改背景顏色和前景顏色,以及逆轉(zhuǎn)顏色方案等。

要更改文本的顏色,請使用 setb 選項(用于設(shè)置背景顏色)和 setf 選項(用于設(shè)置前景顏色)以及在 terminfo 數(shù)據(jù)庫中分配的顏色數(shù)值。通常情況下,分配的數(shù)值與顏色的對應(yīng)關(guān)系如下,但是可能會因 UNIX 系統(tǒng)的不同而異:

執(zhí)行以下示例命令可以將背景顏色更改為黃色,將前景顏色更改為紅色:

tput setb 6 tput setf 4

要反顯當(dāng)前的顏色方案,只需執(zhí)行tput rev

有時,僅為文本著色還不夠,也就是說,您想要通過另一種方式引起用戶的注意。可以通過兩種方式達(dá)到這一目的:一是將文本設(shè)置為粗體,二是為文本添加下劃線。

要將文本更改為粗體,請使用 bold 選項。要開始添加下劃線,請使用 smul 選項。在完成顯示帶下劃線的文本后,請使用 rmul 選項。

實例

使輸出的字符串有顏色,底色,加粗:

#!/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

輸出格式控制函數(shù):

#!/bin/bash

# $1 str       print string
# $2 color     0-7 設(shè)置顏色
# $3 bgcolor   0-7 設(shè)置背景顏色
# $4 bold      0-1 設(shè)置粗體
# $5 underline 0-1 設(shè)置下劃線

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

光標(biāo)屬性例子:

#!/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