使用whiptail开发linux环境交互式对话框

#!/bin/bash

oem=$(/bin/cat /opt/jdwa/etc/oem)
systype=$(/bin/cat /opt/jdwa/etc/systype)
export select_ethname
echo "oem:"$oem
echo "systype:"$systype
CheckIPAddress()
{
    echo $1 |grep "^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$" > /dev/null
    if [ $? = 1 ];  then
        return 1
    else
        a=`echo $1 | awk -F. '{print $1}'`
        b=`echo $1 | awk -F. '{print $2}'`
        c=`echo $1 | awk -F. '{print $3}'`
        d=`echo $1 | awk -F. '{print $4}'`
        for loop in $a $b $c $d
        do
            if [ $loop -gt 255 ] || [ $loop -lt 0 ]; then
                return 2
            fi
        done
    fi  
}

while((1)) 
do
  if [ "$oem"x = "QIANZHI"x ]; then
    if [ "$systype"x = "int"x ]; then
        whiptail --title "Config network" --cancel-button "Quit" --menu "Select Config Network Device " 0 0 2 "man" "" "tun" "" "peer_tun" "" 2> data
    exitstatus=$?
        if [ $exitstatus != 0 ]; then
            break
        fi
        select_ethname=$(/bin/cat data)
        echo "Select Config Network Device:"$select_ethname
    fi
    if [ "$systype"x = "ext"x ]; then
        whiptail --title "Config network" --cancel-button "Quit" --menu "Select Config Network Device " 0 0 2 "tun" "" "peer_tun" "" 2>data
        exitstatus=$?
        if [ $exitstatus != 0 ]; then
            break
        fi
        select_ethname=$(/bin/cat data)
        echo "Select Config Network Device:"$select_ethname
    fi
  fi
  if [ "$oem"x = "COMPONENT"x ]; then
    if [ "$systype"x = "int"x ]; then
        whiptail --title "Config network" --cancel-button "Quit" --menu "Select Config Network Device " 0 0 2 "man" "" 2> data
        exitstatus=$?
        if [ $exitstatus != 0 ]; then
            break
        fi
        select_ethname=$(/bin/cat data)
        echo "Select Config Network Device:"$select_ethname
    fi
    if [ "$systype"x = "ext"x ]; then
        break
    fi
  fi

  if [ "$select_ethname"x = "man"x ] || [ "$select_ethname"x = "tun"x ]; then
      if [ "$select_ethname"x = "man"x ]; then    
          netinfo=$(/bin/cat /opt/jdwa/etc/man)
      else
          netinfo=$(/bin/cat /opt/jdwa/etc/tun0)
      fi
      IFS='/' arr=($netinfo)
      IPaddr=$(whiptail --inputbox "Please input interface "$select_ethname" IpAddr :" 10 60 ${arr[0]}  --title "Config NetWork" 3>&1 1>&2 2>&3)
      exitstatus=$?
      if [ $exitstatus != 0 ]; then
          continue
      fi
      Maskaddr=$(whiptail --inputbox "Please input interface "$select_ethname" MaskAddr :" 10 60 ${arr[1]}  --title "Config NetWork" 3>&1 1>&2 2>&3)
      exitstatus=$?
      if [ $exitstatus != 0 ]; then
          continue
      fi
      GateWay=$(whiptail --inputbox "Please input interface "$select_ethname" GateWay" 10 60 ${arr[2]}  --title "Config NetWork" 3>&1 1>&2 2>&3)
      exitstatus=$?
      if [ $exitstatus != 0 ]; then
          continue
      fi
      if [ "$IPaddr"x = ""x ] || [ "$Maskaddr"x = ""x ]; then
          whiptail  --title "Message Box" --msgbox "Incomplete input information, configuration failed!" 0 0
          continue
      fi
      CheckIPAddress $IPaddr
      if [ $? != 0 ]; then
          whiptail  --title "Message Box" --msgbox "IPaddr Error, configuration failed!" 0 0
          continue
      fi
      CheckIPAddress $Maskaddr
      if [ $? != 0 ]; then
          whiptail  --title "Message Box" --msgbox "Maskaddr Error, configuration failed!" 0 0
          continue
      fi
      if [ "$GateWay"x != ""x ]; then
          CheckIPAddress $GateWay
          if [ $? !=  ]; then
              whiptail  --title "Message Box" --msgbox "GateWay Error, configuration failed!" 0 0
              continue
          fi
      fi 
      if (whiptail --title "Config NetWork" --yesno "Please make sure to save the configuration.
          Devname : "$select_ethname"
          IpAddr  : "$IPaddr"
          MaskAddr: "$Maskaddr"
          GateWay : "$GateWay 0 0) then
          if [ "$select_ethname"x = "man"x ]; then
              echo $IPaddr"/"$Maskaddr"/"$GateWay> /opt/jdwa/etc/man
          else
              echo $IPaddr"/"$Maskaddr"/"$GateWay> /opt/jdwa/etc/tun0
          fi     
      else
          exitstatus=$?
          if [ $exitstatus != 0 ]; then
              continue
          fi
      fi
  fi
  if [ "$select_ethname"x = "peer_tun"x ]; then
      compaddr=$(/bin/cat /opt/jdwa/etc/peer_tun0)
      IPaddr=$(whiptail --inputbox "Please input interface "$select_ethname" IpAddr :" 10 60 $compaddr  --title "Config network" 3>&1 1>&2 2>&3)
      exitstatus=$?
      if [ $exitstatus != 0 ]; then
          continue
      fi
      if [ "$compaddr"x != ""x]; then
          CheckIPAddress $compaddr
          if [ $? != 0 ]; then
              whiptail  --title "Message Box" --msgbox "IPaddr Error, configuration failed!" 0 0
              continue
          fi
      fi
      if (whiptail --title "Config NetWork" --yesno "Please make sure to save the configuration.
          Devname : "$select_ethname"
          IpAddr  : "$IPaddr 0 0) then
          echo $IPaddr> /opt/jdwa/etc/peer_tun0
      else
          exitstatus=$?
          if [ $exitstatus != 0 ]; then
              continue
          fi
      fi

  fi
done
 

相关内容推荐