免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1167 | 回复: 0
打印 上一主题 下一主题

Linux程式设计入门 - Dialog [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-14 10:40 |只看该作者 |倒序浏览
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


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/53051/showart_421836.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP