Linux程式設計入門 - Dialog(轉)

BSDLite發表於2007-08-12
Linux程式設計入門 - Dialog(轉)[@more@]dialog是個shell scripts用的,事實上當您下載Linux Kernel時,裡面有個scripts/lxdialog目錄,其實那就是dialog原始碼,只是Linux kernel為了避免與原有dialog相沖突,將名字修改為lxdialog。當您使用"make menuconfig"時,便是在用dialog這套工具。另外,Slackware的安裝程式,事實上也是用dialog這套工具來做介面的。

您可以利用shell script來呼叫dialog,也可以利用perl來呼叫它,用以提供較友善的使用者介面。

利用dialog這個工具,您可以在不需要寫"艱深"的ncurses的程式的狀況下,使用Shell Script,完成很複雜的操作介面,大大減少產品開發時間。


您可以用"man dialog"來查它的使用方法。這裡做一些dialog的使用及示範。

dialog --clear

整個螢幕會清除後離開

dialog --create-rc file

dialog支援動態規劃,這個功能會產生一個樣本。

dialog [ --title title ] [ --backtitle backtitle ]
[--clear ] [ --separate-output ] box-options

--title title

對話盒上的標題文字

--backtitle backtitle

桌面背景的標題

box-options

dialog目前提供了yes/no box, menu box, input box, message box, text
box, info box, checklist box, radiolist box, 及gauge box共九種widget.

Exit Status

如果按下Yes或OK按鍵,則會返回0。No或Cancel會返回1。如果按下ESC或發生錯誤,則會返回-1。

--yesno text height width

[foxman@foxman /]# dialog --title "hello" --backtitle "Dialog"
--yesno "Is everything okay" 20 60


--msgbox text height width

[foxman@foxman /]# dialog --title "hello" --backtitle "Dialog"
--msgbox "Is everything okay" 20 60


--infobox text height width

[foxman@foxman dialog]# dialog --title "hey" --backtitle "Dialog"
--infobox "Is everything okay?" 10 60

Infobox會在顯示訊息後立即離開,在console的狀況下,訊息會留下,但在X
Terminal下,由於X Terminal會立即將訊息清除,Screen Shot抓不到,因此這
裡沒有ScreenShot。您可以在console下測試。

--inputbox text height width [init]

[foxman@foxman dialog]# dialog --title "hey" --backtitle "Dialog"
--inputbox "Is everything okay?" 10 60 "yes"




--textbox file height width

[foxman@foxman copyright]# dialog --title "Array 30" --backtitle "All
For Chinese" --textbox array30 20 75


textbox很像是個簡單的text viewer,它會顯示檔案中的文字。

--menu text height width menu-height [ tag item ] ...

[foxman@foxman dialog]# dialog --title "Menu Example" --menu "MENU"
20 60 4 tag1 " item1" tag2 "item2" tag3 "item3" tag4 "item4"




--checklist text height width list-height [ tag item status ]
...

[foxman@foxman dialog]# dialog --title "CheckList Example"
--checklist "Check List" 20 60 4 tag1 "item1" on tag2 "item2" off
tag3 "item3" on tag4 "item4" off


--radiolist text height width list-height [ tag item status
] ...

[foxman@foxman dialog]# dialog --title "RadioList Example"
--radiolist "Radio List" 20 60 4 tag1 "item1" on tag2 "item2" off
tag3 "item3" on tag4 "item4" off


--gauge text height width percent

[foxman@foxman dialog]# dialog --title "Installation" --backtitle
"Star Linux" --gauge "Linux Kernel" 10 60 50


gauge widget在啟動後,會從stdin讀取percent進來,讀到EOF時結束。


配合Shell Script進階使用

單單知道每個功能如何使用是還不夠的,一般您要需要知道如何配合Script來使用。
會需要互動的有yesno、inputbox、menu、checklist、radiolist、gauge。

yesno

範例
#!/bin/sh

DIALOG=dialog

if  $DIALOG --title "WCW v.s. NWO" --backtitle "Wrestling"
--yesno "Are you ready to rumble?" 5 60; then
echo "Yeahhhhh"
else
echo "Nap..."
fi

inputbox

範例
#!/bin/sh

DIALOG=dialog

if  $DIALOG --title "The future is here"
--inputbox "Where do you want to go tomorrow?"
10 75 "Penguin" 2>my_own_destiny
then
way=`cat my_own_destiny`
echo "My way is  $way"
else
echo "freak out"
fi

menu

#!/bin/sh

if dialog --title "title"
--menu "MENU" 20 60 14
tag1 "item1" tag2 "item2" 2>select
then
selection=`cat select`
echo "my selection is  $selection"
else
echo "go"
fi

checklist

#!/bin/sh

if dialog --title "title"
--checklist "checklist" 20 60 14
tag1 "item1" on tag2 "item2" off 2>select
then
selections=`cat select`

echo "My selections are:"
for i in  $selections ; do
echo  $i
done

else
echo "go"
fi

radiolist

#!/bin/sh

if dialog --title "title"
--radiolist "checklist" 20 60 14
tag1 "item1" on tag2 "item2" off 2>select
then
selection=`cat select`
echo "My selection is  $selection"
else
echo "go"
fi

gauge

到目前為止,gauge似乎都一直有點問題,檔案上也寫說是有點問題,所附的範例程式也是無法使用,不建議您使用(我自己也玩不出來,你如果有試出來,請來信告訴我)。


OK STATION, Webmaster, Brian Lin

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617542/viewspace-949011/,如需轉載,請註明出處,否則將追究法律責任。

相關文章