shell指令碼之獲取終端資訊

2021-06-27 04:10:25 字數 1196 閱讀 6932

如果要處理大量當前的終端的相關資訊,比如行數,列數,游標位置和遮蓋密碼欄位等,就要用到tput和stty這兩個終端處理工具了.

獲取終端的行數和列數:

tput cols

tput lines

列印當前終端名:

tput longname

將游標移到方位(100,100)處:

tput cpu 100 100

設定終端背景色:

tput setb no  (其中,no可以在0到7之間取值)

將文字前景色設定為白色:

tput serf no  (其中,no可以在0到7之間取值)

設定文字樣式為粗體:

tput bold

設定下劃線的起止:

tput smul

tput rmul

刪除當前游標位置到行尾的所有內容:

tput ed

下面的例子,在輸入密碼的時候,為了安全,不讓輸入的內容顯示出來,將用stty來實現.指令碼內容如下:

#!/bin/bash

for((i=1;i<=3;i++));do

tput setf 3

echo -n -e "enter password: "

stty -echo

read password

if [ $password -eq 123 ];then

echo

echo "password is right!"

stty echo

exit 0

elif [ $i -eq 3 ];then

echo

echo "password is wrong,bye!"

stty echo

else

echo

echo "password is wrong,plsase again!"

stty echo

fidone

測試結果如下圖所示:

1002 獲取終端資訊

1 介紹 當前終端的相關資訊,如行數 列數 游標 位置 密碼等。2 行數和列數 tput cols tput lines 3 列印當前終端名 tput longname 4 將游標移動到座標 100,100 處 tput cup 100 100 5 設定終端背景色 tput setb n n為0到7...

shell獲取指令碼路徑

以下四種 pwd 獲取當前執行 主函式 指令碼的路徑 dirname 0 相對路徑 cd dirname 0 pwd 當前執行指令碼的絕對路徑 cd dirname pwd 指令碼絕對路徑 這段 所在指令碼的絕對路徑 對於第三種情況,它存在的意義在於,當該指令碼被呼叫時,顯示的路徑是這段 所在指令碼...

shell 指令碼之for

subdir joan joanna for subdir in subdir doecho building subdir done 結果 building joan building joanna 結果正常。subdir 1 2 3 4 for subdir in subdir doecho b...