[pisa-src] r935 - trunk/tools

  • From: Diego Biurrun <diego@xxxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Fri, 04 Sep 2009 17:57:43 +0200

Author: biurrun
Date: Fri Sep  4 17:57:43 2009
New Revision: 935

Log:
Replace a lot of if statements by case constructs.

Modified:
   trunk/tools/switch-ip

Modified: trunk/tools/switch-ip
==============================================================================
--- trunk/tools/switch-ip       Fri Sep  4 17:19:50 2009        (r934)
+++ trunk/tools/switch-ip       Fri Sep  4 17:57:43 2009        (r935)
@@ -94,29 +94,24 @@
 ########################
 # Script
 
-CMD=$1
-
-if [ "$CMD" = "--help" ] || [ "$CMD" = "-h" ]; then
-       usage
-       exit 0
-fi
+case $1 in
+    --help|-h)
+        usage
+        exit 0
+        ;;
+    -*|"")
+        CMD="ifconfig"    # choose the default command
+        echo "choosing default command ifconfig!"
+        ;;
+    *)
+        CMD="$1"
+        ;;
+esac
+shift
 
 if [ $(whoami) != "root" ]; then
        echo "You must be root to run this script!"
-       exit 1
-fi
-
-if [ "x$CMD" = "x" ]; then
-       CMD="ifconfig"
-       shift 1
-fi
-
-if [ ! "${CMD#-}" = "${CMD}" ]; then
-       CMD="ifconfig"  # choose the default command
-       echo "choosing default command ifconfig!"
-else
-       # shift $2 to $1 by one step
-       shift 1
+       #exit 1
 fi
 
 optarr=($(getopt -o 'hi:' --long 
'myip1:,myip2:,apip1:,apip2:,apssid1:,apssid2:,apmac1:,apmac2:,dhcp_cmd:,route_extra_cmd:,nowep,help'
 -- "$@"))
@@ -219,15 +214,15 @@
 
 echo "switching $IFWLAN IP from (\"$OLD_SSID\") to $NEW_IP(\"$NEW_SSID\")"
 
-if [ "$CMD" = "arponly" ]; then
+case "$CMD" in
+ arponly)
  echo "iwconfig $IFWLAN $IWCONFIG_OPTIONS"
  iwconfig $IFWLAN $IWCONFIG_OPTIONS
 
  echo "arp -d $AP_IP"
  arp -d $AP_IP 2> /dev/null
-fi
-
-if [ "$CMD" = "ifconfig" ]; then
+ ;;
+ ifconfig)
  echo "iwconfig $IFWLAN $IWCONFIG_OPTIONS"
  iwconfig $IFWLAN $IWCONFIG_OPTIONS
 
@@ -250,9 +245,8 @@
 
 # echo "ip -6 route del $DEST_IPV6 dev $IFDUMMY"
 # ip -6 route del $DEST_IPV6 dev $IFDUMMY 2> /dev/null
-fi
-
-if [ "$CMD" = "dhcp" ]; then
+ ;;
+ dhcp)
  echo "iwconfig $IFWLAN essid $NEW_SSID ap $NEW_MAC_ADR"
  iwconfig $IFWLAN essid "$NEW_SSID" ap "$NEW_MAC_ADR"
 
@@ -268,9 +262,8 @@
 
  echo "route add -net $DESTINATION netmask $NETMASK gw $AP_IP"
  route add -net $DESTINATION netmask $NETMASK gw $AP_IP
-fi
-
-if [ "$CMD" = "channel" ]; then
+ ;;
+ channel)
  echo "iwconfig $IFWLAN essid $NEW_SSID channel 5"
  iwconfig $IFWLAN essid $NEW_SSID channel 5
 
@@ -281,8 +274,8 @@
  echo "route add -net $DESTINATION netmask $NETMASK gw $AP_IP"
  route del -net $DESTINATION netmask $NETMASK 2> /dev/null
  route add -net $DESTINATION netmask $NETMASK gw $AP_IP 2> /dev/null
-fi
-
+ ;;
+esac
 
 
 exit 0

Other related posts:

  • » [pisa-src] r935 - trunk/tools - Diego Biurrun