[shell]shell脚本传入不固定参数的写法,如--help等

最近在调试一个wifi模块,需要传一些不固定的参数,一下一个参数解析的函数可以搞定这个事情,里面内容好多部分是从一个example中剽窃而来(窃喜)

#!/bin/bash
# writen by aaron.gao at 2017.06.22

arg_ssid="ssid-test"
arg_passphrase="passphrase-test"
arg_ifip="192.168.4.1"
arg_stop="0"
arg_start="0"
arg_help="0"


print_help() {
  cat <<EOF
  use $program --ssid=<ssid>

    --ssid=<ssid> - where <ssid> is the name of the AP advertised with the beacon frames. (default:$arg_ssid)
    --passphrase=<passphrase> - where <passphrase> is the password of AP (default:$arg_passphrase)

    --stop  - stop accesspoint
    --start - also start accesspoint
    --help  - prints help screen


EOF
}

parse_arguments() {
  local helperKey="";
  local helperValue="";
  local current="";

  while [ "$1" != "" ]; do
      current=$1;
      helperKey=${current#*--};
      helperKey=${helperKey%%=*};
      helperKey=$(echo "$helperKey" | tr '-' '_');
      helperValue=${current#*=};
      if [ "$helperValue" == "$current" ]; then
        helperValue="1";
      fi
      #echo "eval arg_$helperKey="$helperValue"";

      eval "arg_$helperKey="$helperValue"";
      shift
  done
}

parse_arguments ${@};

if [ "$arg_help" != "0" ]; then
    print_help;
     exit 1;
fi


if [ "$arg_stop" == "1" ]; then
    kill $(pidof -o %PPID -x $(basename $0)) 
    ./uaputl.exe bss_stop

    ifconfig uap0 down;
    exit 0
fi

ifconfig uap0 up;

# echo "set ssid and passwd"
./uaputl.exe sys_cfg_ssid $arg_ssid
./uaputl.exe sys_cfg_auth 0
./uaputl.exe sys_cfg_protocol 32 #sets WPA2 protocol
./uaputl.exe sys_cfg_wpa_passphrase $arg_passphrase
./uaputl.exe sys_cfg_cipher 8 8 #PAIRWISE_CIPHER:AES CCMP GROUP_CIPHER:AES CCMP

# echo "set RF params"
./uaputl.exe sys_cfg_channel 48 4 #Set AP primary radio channel to 48, and
# secondary channel is below.
./uaputl.exe sys_cfg_2040_coex 1 #enable 20/40 BSS coexistence Configuration
./uaputl.exe sys_cfg_11n 1 0x116e 3 0 0xff
./uaputl.exe sys_cfg_rates 0xc 0x12 0x18 0x24 0x30 0x48 0x60 0x6c

# echo "set regulation domain"
./uaputl.exe sys_cfg_80211d state 1 country DE

if [ "$arg_start" == "1" ]; then
  sleep 1;
  ./uaputl.exe bss_start

  ifconfig uap0 "$arg_ifip"

fi

exit 0;
原文地址:https://www.cnblogs.com/aaronLinux/p/7066818.html