標籤:
#!/bin/sh#說明show_usage="args: [-i , -p , -u , -w , -a , -s , -d , -v ] [--ip=, --port=, --user=, --pwd=, --path=, --script=, --debug=, --version=]"#參數opt_ip=""opt_port=""opt_user=""opt_pwd=""opt_path=""opt_script=""opt_debug=""opt_version=""GETOPT_ARGS=`getopt -o i:p:u:w:a:s:d:v: -al ip:,port:,user:,pwd:,path:,script:,debug:,version: -- "[email protected]"`eval set -- "$GETOPT_ARGS"#擷取參數while [ -n "$1" ]docase "$1" in-i|--ip) opt_ip=$2; shift 2;;-p|--port) opt_port=$2; shift 2;;-u|--user) opt_user=$2; shift 2;;-w|--pwd) opt_pwd=$2; shift 2;;-a|--path) opt_path=$2; shift 2;;-s|--script) opt_script=$2; shift 2;;-d|--debug) opt_debug=$2; shift 2;;-v|--version) opt_version=$2; shift 2;;--) break ;;*) echo $1,$2,$show_usage; break ;;esacdoneif [[ -z $opt_ip || -z $opt_port || -z $opt_user || -z $opt_pwd || -z $opt_path || -z $opt_script || -z $opt_debug || -z $opt_version ]]; thenecho $show_usageecho "opt_ip:"$opt_ip",opt_port:"$opt_port",opt_user:"$opt_user",opt_pwd:"$opt_pwd",opt_path:"$opt_path",opt_script:"$opt_script",opt_debug:"$opt_debug",opt_version:"$opt_versionexit 0fi#開始處理#ip port user pwd 串連伺服器#script path debug version 作為參數執行
有幾個關鍵會被忽略導致失敗的地方:
eval set -- "$GETOPT_ARGS" eval關鍵字是必須的,網上好多例子沒有,坑爹
GETOPT_ARGS=`getopt -o i:p:u:w:a:s:d:v: -al ip:,port:,user:,pwd:,path:,script:,debug:,version: -- "[email protected]"`
最後的--也是必須的,網上好多例子也沒有,坑爹
shell getopt 擷取參數