shell getopt 擷取參數

來源:互聯網
上載者:User

標籤:

#!/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 擷取參數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.