红联Linux门户
Linux帮助

Linux程式设计入门 - Dialog

发布时间:2006-03-29 11:17:58来源:红联作者:[Hl.y]
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
文章评论

共有 0 条评论