免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5568 | 回复: 10

一段基于xdialog对话框的shell脚本欣赏[转] [复制链接]

论坛徽章:
0
发表于 2004-09-30 23:42 |显示全部楼层

  1. #!/bin/sh
  2. #
  3. # pppdialout
  4. #
  5. #DIALOG=Xdialog
  6. DIALOG=dialog
  7. #
  8. # 你的默认ISP的名字:
  9. defaultisp=maxnet
  10. #
  11. error()
  12. {
  13.     echo "$1"
  14.     exit 2
  15. }

  16. help()
  17. {
  18.   cat <<HELP
  19. pppdialout -- select an ISP and dial out.
  20. All available ISPs must have a config file in /etc/ppp/peers
  21. pppdialout executes the ppp-on/ppp-off scripts as described
  22. in http://linuxfocus.org/English/March2001/article192.shtml
  23. pppdialout, copyright gpl, http://linuxfocus.org/English/November2002
  24. HELP
  25.   exit 0
  26. }
  27. # 分析命令行参数:
  28. while [ -n "$1" ]; do
  29. case $1 in
  30.     -h) help;shift 1;; # function help is called
  31.     --) shift;break;; # end of options
  32.     -*) echo "error: no such option $1. -h for help";exit 1;;
  33.     *)  break;;
  34. esac
  35. done
  36. tempfile=/tmp/pppdialout.$$
  37. trap "rm -f $tempfile" 1 2 5 15

  38. # 检查是否已经连接internet
  39. if /sbin/ifconfig | grep '^ppp' > /dev/null; then
  40.     # 我们已经在线
  41.     $DIALOG --title "go offline" --yesno "Click YES to \
  42.                      terminate the ppp connection" 0 0
  43.     rval="$?"
  44.     clear
  45.     if [ "$rval" = "0" ]; then
  46.         echo "running /etc/ppp/scripts/ppp-off ..."
  47.         /etc/ppp/scripts/ppp-off
  48.     fi
  49. else
  50.     # 没发现internet连接, 进行连接
  51.     # 从 /etc/ppp/peers 取得可用的ISP列表
  52.     for f in `ls /etc/ppp/peers`; do
  53.         if [ -f "/etc/ppp/peers/$f" ]; then
  54.             isplist="$isplist $f =="
  55.         fi
  56.     done
  57.     [ -z "$isplist" ]&&error "No isp def found in /etc/ppp/peers"
  58.     #
  59.     $DIALOG --default-item "$defaultisp" --title "pppdialout"  \
  60.          --menu "Please select one of\
  61.     the following ISPs for dialout" 0 0 0 $isplist 2> $tempfile
  62.     rval="$?" # return status, isp name will be in $tempfile
  63.     clear
  64.     if [ "$rval" = "0" ]; then
  65.         isp=`cat $tempfile`
  66.         echo "running /etc/ppp/scripts/ppp-on $isp..."  
  67.         /etc/ppp/scripts/ppp-on "$isp"
  68.     else
  69.         echo "Cancel..."   
  70.     fi
  71.     rm -f $tempfile
  72. fi
  73. # pppdialout 结束
复制代码

这个脚本是如何工作的:
开始,我们定义了一些函数:error和help, 下一步我们检查命令行参数,然后定义了一相临时文件(/tmp/pppdialout.$$). $$是当前进行的名字,它在每一个计算机中都是独立的. 陷井语句在程序被强制中断(如用户按下Ctrl+C)是执行并且删除我们定义的临时文件. 然后我检查我们是否已经在线(命令:/sbin/ifconfig | grep '^ppp'). 如果我们已经在线则打开一个对话框, 就像上面你所看到的那样, 询问用户是否要断连接. 如果我们不在线就打开一个菜单对话框. 我们从 /etc/ppp/peers (ls /etc/ppp/peers)中取得所有可用的ISPs. 菜单对话框的语法是:


dialog --menu "text" <height> <width> <menu height> <tag1> <description> ...
<height>, <width> 和<menu height> 被设置为0(自动调整,请看上面),然后是程序预定的语法格式的字符串(<tag1> <description>. 我们没有什么描述,就用了一些无意义的东西(==). 变量ipslist中的数据看起来像这样:


isp1 == isp2 == isp3 ==

用户的选择结果会被打印到标准错误. 命令"2> $tmpfile" 把这一结果写到我们的临时文件(tmpfile)里. 这个菜单对话框有cancel可选, 所以我们必须检查 $? (退出状态) 以确定那一个键被按下.

好啦, 理论已经足够. 结果看起来是这样:



from: http://www.linuxfocus.org/ChineseGB/November2002/article267.shtml

论坛徽章:
0
发表于 2004-10-01 11:21 |显示全部楼层

一段基于xdialog对话框的shell脚本欣赏[转]

不错不错,顶~~~

论坛徽章:
0
发表于 2006-01-15 16:38 |显示全部楼层
好东西,要顶

论坛徽章:
0
发表于 2006-01-15 16:50 |显示全部楼层
原帖由 hbird 于 2006-1-15 16:38 发表
好东西,要顶


..................

论坛徽章:
0
发表于 2006-01-15 17:17 |显示全部楼层
如果有安裝Xdialog, 在 /usr/share/doc/xdialog-2.1.2/samples/ 有幾個腳本,還有一個寫得很好的
教學, 這個是它的 editbox sample 腳本
#!/bin/sh

cat << EOF > /tmp/editbox.tmp.$$
Hi, this is an edit box. It can be used to edit text from a file.

It's like a simple text editor, with these keys implemented:

PGDN    - Move down one page
PGUP    - Move up one page
DOWN    - Move down one line
UP      - Move up one line
DELETE  - Delete the current character
BACKSPC - Delete the previous character
CTRL C  - Copy text
CTRL V  - Paste text

Mouse selection pasting (selecting text to be copied with left mouse
button held down and pasting by clicking the middle button mouse) is
also available.

Try to input some text below:

EOF
Xdialog --title "EDIT BOX" --fixed-font -print "" \
        --editbox /tmp/editbox.tmp.$$ 0 0 2>/tmp/editbox.txt.$$

case $? in
  0)
    cat /tmp/editbox.txt.$$
    echo "OK"
    ;;
  1)
    echo "Cancel pressed."
    ;;
  255)
    echo "Box closed."
    ;;
esac

rm -f /tmp/editbox.tmp.$$
rm -f /tmp/editbox.txt.$$

论坛徽章:
0
发表于 2006-01-15 17:21 |显示全部楼层
这个在 命令行格式的介面上可以显现成您这样的图样 吗?

主要是不明白这点.

论坛徽章:
0
发表于 2006-01-15 17:23 |显示全部楼层
原帖由 pvb1979 于 2006-1-15 17:21 发表
这个在 命令行格式的介面上可以显现成您这样的图样 吗?

主要是不明白这点.

估计应该装X客户端,只在字符界面应该不成的

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
发表于 2006-01-15 17:32 |显示全部楼层
原帖由 大蚂蚁 于 2006-1-15 17:23 发表

估计应该装X客户端,只在字符界面应该不成的

dialog ^_^

论坛徽章:
0
发表于 2006-01-15 17:32 |显示全部楼层
這個我寫的練習,用 Xdialog 的 textbox 顯示 man page ,

#! /bin/bash
pn=${0##*/}
temp="$HOME/manual.$$"
trap "rm -f $temp" 0 1 2 9
prog=Xdialog
str="輸入檢視的指令"
if (( $# == 0 )) ; then
   $prog --title "$pn" --backtitle "$str" --inputbox "" 0 0 2> $temp
else
   arg=$1 ;
   echo "$arg" > $temp
fi
    retval=$?
    case "$retval" in
            0)
              i=$(<$temp) ;
              [ -n "$i" ] || {
              echo "Null string, $pn exit" ; exit 1 ;
              }
              if man $i > /dev/null 2>&1 ; then
              man $i | col -b  > $temp ;
              else
              echo "$i:  no such manual page." >&2
              exit 1 ;
              fi
              ;;
       1 | 255)
              echo "Abort." ;
              exit 1 ;
              ;;
     esac
     #printer button not tested cause no printer
  $prog --title "$pn" --no-cancel --print "lpr -P lp0" --textbox "$temp" 20 90
   rm -f $temp
screen.jpg

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
发表于 2006-01-15 19:17 |显示全部楼层
原帖由 twf_cc 于 2006-1-15 17:32 发表
這個我寫的練習,用 Xdialog 的 textbox 顯示 man page ,

#! /bin/bash
pn=${0##*/}
temp="$HOME/manual.$$"
trap "rm -f $temp" 0 1 2 9
prog=Xdialog
str="輸入檢視的指令&qu ...

good,其实,有情趣的话,可以用这个xdialog写个mc,^_^,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP