天天看點

圖形環境下的shell程式設計selectdialogtputnotify-sendkdialogzenity

目錄

  • select
  • dialog
  • tput
  • notify-send
  • kdialog
  • zenity

select

select指令可以建立很簡單的菜單,然後擷取輸入的答案.

select指令的格式為:

select variable in list
do
    commands
done
           

其中list參數是由空格分隔的文本選項清單,這些清單構成了整個菜單. select指令會将每個清單現實成一個帶編号的選項,然後為選項顯示一個由PS3環境變量定義的提示符.

例如我們可以建立一個簡單的檔案選擇腳本:

if [ $# -eq 0 ];then
    file=$(pwd)
else
    file=$1
fi

if [ -d $file ];then
    select f in $(ls $file)
    do
        exec $0 $file/$f
    done
else
    echo $file
fi

[email protected]:~$ file_selector.sh bin
 1) -          17) git-sync.sh
 2) autoload         18) mail2org.sh
 3) cleanFiles.pl      19) mgrep.sh
 4) develop        20) monitor_network.sh
 5) dired.sh         21) mpg123.sh
 6) download_comic_from_avbebe.sh  22) my-byte-split.sh
 7) ec           23) my-line-split.sh
 8) ediff.sh         24) pick.sh
 9) emacsclient.sh       25) ptree.sh
10) emages.sh        26) rcsvi.sh,v
11) es           27) redo.sh
12) eshell.sh        28) shr
13) et           29) testClean.cfg
14) file_selector.sh       30) vir
15) gclsg.lisp.tw      31) wget-website.sh
16) git-push.sh
#? 2
1) git-pull.sh
2) study.sh
#? 1
bin/autoload/git-pull.sh
           

dialog

dialog能夠使用ANSI轉義控制字元從文本環境建立标準的視窗對話框.

下面是用用dialog來重寫的file_selector.sh

if [ $# -eq 0 ];then
    file=$(pwd)
else
    file=$1
fi

file=$(dialog --title "file selector" --stdout --fselect $file 10 50)

if [ -d $file ];then
    exec $0 $file
else
    echo $file
fi
           
圖形環境下的shell程式設計selectdialogtputnotify-sendkdialogzenity

tput

tput 指令将通過 terminfo 資料庫對您的終端會話進行初始化和操作.

通過使用 tput,您可以更改幾項終端功能:如移動或更改光标、更改文本屬性,以及清除終端螢幕的特定區域等.

下面是一個例子:

#!/bin/bash

BOLD=$(tput bold)
REV=$(tput rev)
NORMAL=$(tput sgr0)
CURSOR_OFF=$(tput civis)
CURSOR_ON=$(tput cnorm)

tput init

tput clear
echo $CURSOR_OFF
tput cup 2 15
echo  "${BOLD}粗體效果{NORMAL}\n"
echo  "${REV}反轉效果${NORMAL}"
echo $CURSOR_ON
           
圖形環境下的shell程式設計selectdialogtputnotify-sendkdialogzenity

notify-send

notify-send讓你通過通知程序發送一個桌面通知給使用者.

例如下面是一個檢測網絡的腳本.

#!/bin/bash
target="www.baidu.com"
port=80
interval=60                     # 秒

function live_p()
{
    local remote=$1
    local port=$2
    if timeout 5 echo >/dev/tcp/$remote/$port;then
        echo "網絡通了!"
    else
        echo "網絡不通了!"
    fi
}

while :
do
    current_state=$(live_p $target $port)
    if [ "$current_state" != "$old_state" ];then
        notify-send $current_state
    fi
    old_state=$current_state;
    sleep $interval
done
           

網絡不通的情況下,它會顯示一個視窗告訴你”網絡不通了!”,網絡恢複之後,它又會顯示一個視窗告訴你”網絡通了”

圖形環境下的shell程式設計selectdialogtputnotify-sendkdialogzenity

kdialog

kdialog為KDE桌面提供了類似dialog式的标準視窗. 其生成的視窗能和其他KDE視窗很好的融合.

關于kdialog的使用方法可以參見https://techbase.kde.org/Development/Tutorials/Shell_Scripting_with_KDE_Dialogs

kdialog的file_selector.sh可以是這樣的

if [ $# -eq 0 ];then
    file=$(pwd)
else
    file=$1
fi


if [ -d $file ];then
    file=$(kdialog  --getopenfilename $file)
fi
echo $file
           
圖形環境下的shell程式設計selectdialogtputnotify-sendkdialogzenity

zenity

zenity 是gnome版的dialog. 它針對不同的對話框選項有不同的參數:

對話框選項

  • –calendar: 顯示月曆對話框
  • –entry: 顯示文字輸入欄對話框
  • –error: 顯示錯誤資訊對話框
  • –file-selection: 顯示檔案選擇對話框
  • –info: 顯示資訊對話框
  • –list: 顯示清單對話框
  • –progress: 顯示進度訓示窗
  • –question: 顯示提問資訊對話框
  • –text-info: 顯示文字資信對話框
  • –warning: 顯示警告資訊對話框

一般選項

  • –title=标題: 指定對話框的标題
  • –window-icon=圖示路徑: 指定視窗使用的圖示
  • –width=寬度: 指定寬度
  • –height=高度: 指定高度

月曆選項

  • –text=STRING: 指定對話框中的文字
  • –day=INT: 指定月曆中的日期
  • –month=INT: 指定月曆中的月份
  • –year=INT: 指定月曆中的年份
  • –date-format=STRING: 指定回傳的日期格式

文字輸入欄選項

  • –text=STRING: 指定對話框中的文字
  • –entry-text=STRING: 指定輸入欄中的文字
  • –hide-text: 隐藏輸入欄中的文字

錯誤資訊選項

  • –text=STRING: 指定對話框中的文字

有關檔案選擇的選項

  • –filename=檔案名稱: 指定檔案名稱
  • –multiple: 允許同時選擇多個檔案
  • –separator=分隔字元: 指定分隔輸出結果的字元。

資訊選項

  • –text=STRING: 指定對話框中的文字

清單選項

  • –column=STRING: 指定欄位标題
  • –checklist: 第一欄使用獨立選擇按鈕
  • –radiolist: 第一欄使用多項選擇按鈕
  • –separator=分隔字元: 指定分隔輸出結果的字元
  • –editable: 可以更改文字

進度選項

  • –text=STRING: 指定對話框中的文字
  • –percentage=INT: 指定開始時的百份比數值
  • –pulsate: 填滿進度列
  • –auto-close: 當進度達到100% 時關閉對話框

提問選項

  • –text=STRING: 指定對話框中的文字

文字選項

  • –filename=檔案名稱: 從檔案中載入文本
  • –editable: 可以更改文字

警告資訊選項

  • –text=STRING: 指定對話框中的文字

GTK+ 選項

  • –gdk-debug=标簽: 準備設定的gdk 調試标簽
  • –gdk-no-debug=标簽: 準備去除的gdk 調試标簽
  • –display=畫面: 準備使用的 X 畫面
  • –sync: 進行 X 同步呼叫
  • –name=名稱: 視窗總管所需的程式名稱
  • –class=類别: 視窗總管所需的程式類别名稱

–gxid-host=主機

–gxid-port=端口号

  • –gtk-debug=标簽: 準備設定的gtk+ 調試标簽
  • –gtk-no-debug=标簽: 準備去除的gtk+ 調試驗标簽
  • –g-fatal-warnings: 将所有警告資訊作為嚴重錯誤處理
  • –gtk-module=子產品: 載入額外的 Gtk 子產品

zennity版本的file_selector.sh是這樣的

if [ $# -eq 0 ];then
    file=$(pwd)
else
    file=$1
fi


if [ -d $file ];then
    cd $file
    file=$(zenity  --file-selection)
fi
echo $file
           
圖形環境下的shell程式設計selectdialogtputnotify-sendkdialogzenity