Shell脚本支持同时使用多个参数 shift移动参数列表
在前面的修改中,我们的脚本已经支持了gh-proxy参数。
但是warp脚本还支持比如 4, 6, s5 等参数比较方便使用,不用在菜单里选了。
所以我们的脚本能支持多个参数就好了。
这里就要用到 shift 来移动参数列表
参考:
https://github.com/czyt1988/czyBlog/tree/master/techShell/shift-getopt
在每一个判断参数的case项里面,要用shift移走一个参数“段”。如果这个参数是不带详细参数的,那么就shift 1移走一个参数。如果这个参数是带详细参数的,那么就要shift 2移走两个参数。
用一个判断参数数目的循环保证一直处理到参数都没有了。
以下实践没有判断异常情况。如果使用者不按格式使用参数,可能会出现不确定的后果。
修改前:
if [ $# -ge 1 ]; then
Get_System_Info
case ${1} in
install)
Install_WARP_Client
;;
#略
*)
log ERROR "Invalid Parameters: $*"
Print_Usage
exit 1
;;
esac
else
Print_Usage
fi修改后:
if [ $# -ge 1 ]; then Get_System_Info
until [ $# -eq 0 ] do case ${1} in install) Install_WARP_Client
shift 1 ;;
#略
ghproxy) ghproxy=${2} shift 2 ;;
*) log ERROR "Invalid Parameters: $*" Print_Usage exit 1 ;; esac
done else Print_Usage fi
使用方法:
./warp.sh ghproxy "https://github.crazypeace.workers.dev/" menu不带ghproxy参数的用法:
./warp.sh menu
