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

时间:2023-03-09 03:43:18
[shell]shell脚本传入不固定参数的写法,如--help等

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

#!/bin/bash
# writen by aaron.gao at 2017.06. arg_ssid="ssid-test"
arg_passphrase="passphrase-test"
arg_ifip="192.168.4.1"
arg_stop=""
arg_start=""
arg_help="" 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=$;
helperKey=${current#*--};
helperKey=${helperKey%%=*};
helperKey=$(echo "$helperKey" | tr '-' '_');
helperValue=${current#*=};
if [ "$helperValue" == "$current" ]; then
helperValue="";
fi
#echo "eval arg_$helperKey=\"$helperValue\""; eval "arg_$helperKey=\"$helperValue\"";
shift
done
} parse_arguments ${@}; if [ "$arg_help" != "" ]; then
print_help;
exit ;
fi if [ "$arg_stop" == "" ]; then
kill $(pidof -o %PPID -x $(basename $))
./uaputl.exe bss_stop ifconfig uap0 down;
exit
fi ifconfig uap0 up; # echo "set ssid and passwd"
./uaputl.exe sys_cfg_ssid $arg_ssid
./uaputl.exe sys_cfg_auth
./uaputl.exe sys_cfg_protocol #sets WPA2 protocol
./uaputl.exe sys_cfg_wpa_passphrase $arg_passphrase
./uaputl.exe sys_cfg_cipher #PAIRWISE_CIPHER:AES CCMP GROUP_CIPHER:AES CCMP # echo "set RF params"
./uaputl.exe sys_cfg_channel #Set AP primary radio channel to , and
# secondary channel is below.
./uaputl.exe sys_cfg_2040_coex #enable / BSS coexistence Configuration
./uaputl.exe sys_cfg_11n 0x116e 0xff
./uaputl.exe sys_cfg_rates 0xc 0x12 0x18 0x24 0x30 0x48 0x60 0x6c # echo "set regulation domain"
./uaputl.exe sys_cfg_80211d state country DE if [ "$arg_start" == "" ]; then
sleep ;
./uaputl.exe bss_start ifconfig uap0 "$arg_ifip" fi exit ;