[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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM