[17]shell界面案例及自动检查系统备份数据库脚本部分实现

一, 用户界面和dialog工具
1,
在文本控制台下实现用户界面,支持 ,多选框,表单,文件选择 进度条 输入框  裁断 消息框 密码 单选框 文本框 Yes No 等多个元件
2,
安装包  dialog
/usr/share/doc/dialog -/sample
目录下提供示例程序
yum install dialog
3,
可以预先查看执行程序的位置
[root@rhel dialog-1.0.20051107]# pwd
/usr/share/doc/dialog-1.0.20051107/samples
[root@rhel dialog-1.0.20051107]#

二, 用户界面和dialog工具
1,
dialog显示文本用户界面,在选择之后可以对其推出状态使用case语句做检测
通常为以下几种:
0                     Yes 或 Ok
1                     No或Cancel
2                     用户点击Help帮助按钮
3                     点击其他的按钮
-1                    Esc键推出
2,
每一次使用一种对话框元素,可以使用下面的语句清除屏幕的同时保留原背景色。dialog --clear;不要使用Shell 命令clear
[root@rhel samples]# dialog --help | less
[root@rhel samples]#
3,
(1) 错误
[root@rhel samples]# dialog --title "Demostration" \
>  --msgbox
Error: Expected at least 3 tokens for --msgbox, have 0.
Use --help to list options.
[root@rhel samples]#
[root@rhel samples]# dialog --title "Demostration" --msgbox "This is a demostration of message \
box" 6 30
(2)
正常
[root@rhel samples]# dialog --title "Demostration" --msgbox "This is a demostration of message \n \
box" 9 50
[root@rhel samples]# dialog --title "Demostration" --msgbox "$(cat /etc/inittab)" 9 50

三,
显示方框
[root@rhel helinbash]# cat showmessagebox.sh
#!/bin/bash
dialog --clear
dialog --backtitle "zhanghelin.com"  --title "Demostration" \
       --msgbox "$(cat /etc/inittab)" 9 50
[root@rhel helinbash]#
注意中途推出是-1也就是255

四, 判断框
[root@rhel helinbash]# cat  deletefile.sh    
#!/bin/bash
FILENAME=/tmp/a.txt

showmsg()
{
   MESSAGE=$1
  dialog --msgbox "$MESSAGE" 9 50

}

dialog --clear
dialog --backtitle "zhanghelin.com" \
       --yesno "Are you sure to delete the file : $FILENAME ?" 9 50
RES=$?

case $RES in

        0)
        [ -f $FILENAME ] && rm $FILENAME -fv || \
        showmsg "The file has been removed"  
        rm  $FILENAME -fv
        ;;
        
        *)

         showmsg "You cancelled"
        ;;


        255)
        showmsg "You pressed escape"
esac       
[root@rhel helinbash]#

五, dialog工具实现输入框
[root@rhel helinbash]# dialog --help | grep inputbox
  --inputbox     []
[root@rhel helinbash]#

$$表示当前执行的bash环境的进程ID

[root@rhel helinbash]# cat delete.sh
#!/bin/bash

TMPFILE="/tmp/$$.txt"

dialog --clear
dialog  --backtitle "zhanghein.com" \
        --inputbox "Please input a filename" 9 50 \
        2> $TMPFILE

FILENAME=$(cat $TMPFILE)

echo  " $FILENAME would be delete "
[root@rhel helinbash]#

六, dialog中的菜单
1,
在dialog中可以实现上下选择菜单的项
其结果输出到一个临时文件,再将tag结果显示出来
语法
2,
[root@rhel helinbash]# dialog --help | grep menu
  --inputmenu   
  --menu        
Global-auto-size if also menu_height/list_height = 0.
[root@rhel helinbash]#
3,
自动检查系统管理备份数据库脚本步骤级部分实现
第一步扫描系统
第二步备份数据库
第三步清除垃圾文件
第四步.....
第五步.....
(1)

[root@rhel helinbash]# cat dailymenu.sh
#!/bin/bash

TMPFILE=/tmp/menu.txt

dialog --title "zhanghelin.com" \
       --menu  "PLS choose one job" 20 60 6 \
       "clean" "Clean temp files" \
       "scan"  "Scanning" \
       "backup" "Backup database" 2> $TMPFILE


JOB=$(cat $TMPFILE)
case $JOB in

      "clean")
               bash ./clean.sh
               ;;
       "scan")
               bash ./scan.sh

               ;;
      "backup")
               bash ./backup.sh

               ;;


esac

[root@rhel helinbash]#
(2)
[root@rhel helinbash]# cat clean.sh
#!/bin/bash

dialog --title "zhanghelin.com" --msgbox "Cleanning now...." 9 50
[root@rhel helinbash]#


[root@rhel helinbash]# cat backup.sh      
#!/bin/bash

dialog --title "zhanghelin.com" --msgbox "Backuping now...." 9 50
[root@rhel helinbash]#


[root@rhel helinbash]# cat scan.sh        
#!/bin/bash

dialog --title "zhanghelin.com" --msgbox "Scanning now...." 9 50
[root@rhel helinbash]#

七, dialog 中的多选框
1,
checklist 与Menu Box 雷同,提供了多选框,同时也提供了选项的on/off的初始状态。
2,
语法
[root@rhel helinbash]# dialog --helop | grep checklist
  --checklist    ...
[root@rhel helinbash]#
3,
[root@rhel helinbash]# cat /tmp/srv.txt
"ntp" "sendmail" "vsftpd"[root@rhel helinbash]#
4,
[root@rhel helinbash]# cat chkconfig.sh
#!/bin/bash

TMPFILE=/tmp/srv.txt

dialog --title "zhanghelin.com" \
       --checklist "Please choose service to disable or enable" 15 60 5 \
       "ntp" "Network timiing protocol" off \
       "sendmail" "Send mail" on \
       "vsftpd" "Very secure FTP" off 2> $TMPFILE
[root@rhel helinbash]#

八, dialog中文件选择框
[root@rhel helinbash]# dialog --help | grep fselect
  --fselect     
[root@rhel helinbash]#

[root@rhel helinbash]# cat fileselect.sh
#!/bin/bash

TMPFILE=/dirtest/aa.txt
dialog --title "Pick on file" --fselect /helinshell 7 40 2>$TMPFILE
[root@rhel helinbash]#
[root@rhel helinbash]# cat /dirtest/aa.txt
/helinbash[root@rhel helinbash]#

九, dialog进度条
[root@rhel tmp]# dialog --help | grep gauge
  --gauge        []
[root@rhel tmp]#
[root@rhel dirtest]# for i in {1..100} ; do echo $i; done | dialog --title "installation " --gauge "installation" 10 30
[root@rhel helinbash]# cat gauge.sh
#!/bin/bash
let PERCENT=0
(

for I in /etc/*
do
if [ $PERCENT -gt 100 ] ; then
     break
else
     cp  $I /dirtest/ 2> /dev/null
     echo "Copy the file $I..."
     echo $PERCENT
fi
PERCENT=$[ $PERCENT + 1 ]
sleep 1
done ) |  dialog --backtitle "zhanghelin.com"  --title  "Copying /etc/* file " --gauge  "Starting to copy files ..." 6 50  
[root@rhel helinbash]#


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29611940/viewspace-1171532/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29611940/viewspace-1171532/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值