2017-06-24 6 views
-1

bashのいくつかのオプションを解析するためにgetopt(1)を使用し、cmd行からオプションを渡すときに評価しません。それはをデフォルトされて - 私はわからない、なぜそれがこの奇妙な行動を引き起こしている、それらを供給したときに)「ダブルダッシュ」case文やその他のオプションを渡して、以下のコードは:奇数Getopt(1)の解析動作

parse_args() { 
    cmd=""    # Default none 
    isparsed=0   # If args parsed = 1 

    # Check the number of arguments. If none are passed, print help and exit 
    options=$(getopt -o hs:d:c:p:i: -l help,source:,destination:,connection:,platform:,install: -n "cdr" -- "[email protected]") 

    # If the number of arguments after processing them using getopt is larger 
    # than zero, then it means there is an unknown argument. 
    if [[ $? -ne 0 ]]; then 
     HELP 
     exit 1 
    fi 

    printdbg "parsing options" 
    eval set -- "$options" 
    while true; do 
     case "$1" in 
      -h) # Show help option menu 
       HELP 
       exit 1 
       ;; 
      -s|--source) # Parse options to source sub command 
       cmd=$2 # Remove 'source' from the argument list 
       shift; 
       if [[ $cmd == "sqlite" ]]; then 
        SOURCE="sqlite" 
       elif [[ $cmd == "mysql" ]]; then 
        SOURCE="mysql" 
       else 
        HELP 
        exit 1 
       fi 
       isparsed=1 
       ;; 
      -d|--destination) # Parse options to destination sub command 
       cmd=$2 # Remove 'destination' from the argument list 
       shift; 
       if [[ $cmd == "postgre" ]]; then 
        DESTINATION="postgre" 
       elif [[ $cmd == "riak" ]]; then 
        DESTINATION="riak" 
       elif [[ $cmd == "both" ]]; then 
        DESTINATION="both" 
       else 
        HELP 
        exit 1 
       fi 
       isparsed=1 
       ;; 
      -c|--connection) # Parse options to connection sub command 
       cmd=$2 # Remove 'connection' from the argument list 
       shift; printdbg "$cmd" 
       if [[ ! -z $cmd ]]; then 
        SOURCE_CONN=$(echo "$cmd" | awk -F "::" '{print $1}') 
        DESTINATION_CONN=$(echo "$cmd" | awk -F "::" '{print $2}') 
        parse_csv "$SOURCE_CONN" #stored in PARSED_ARR 
        echo ${PARSED_ARR[@]} 
#     ${DESTINATION_CONN:=${PARSED_ARR}} 
       else 
        HELP 
        exit 1 
       fi 
       isparsed=1 
       ;; 
      -p|--platform) # Parse options to platform sub command 
       cmd=$2 # Remove 'platform' from the argument list 
       shift; 
       if [[ $cmd == "csv" ]]; then 
        CDR_TYPE=1 
       elif [[ $cmd == "api" ]]; then 
        CDR_TYPE=2 
       elif [[ $cmd == "freeswitch" ]]; then 
        CDR_TYPE=3 
       elif [[ $cmd == "asterisk" ]]; then 
        CDR_TYPE=4 
       elif [[ $cmd == "yate" ]]; then 
        CDR_TYPE=5 
       elif [[ $cmd == "kamailio" ]]; then 
        CDR_TYPE=6 
       elif [[ $cmd == "opensips" ]]; then 
        CDR_TYPE=7 
       elif [[ $cmd == "sipwise" ]]; then 
        CDR_TYPE=8 
       elif [[ $cmd == "veraz" ]]; then 
        CDR_TYPE=9 
       else 
        HELP 
        exit 1 
       fi 
       isparsed=1 
       ;; 
      -i|--install) # Parse options to install sub command 
       cmd=$2 # Remove 'install' from the argument list 
       shift; 
       if [[ $cmd == "sqlite" ]]; then 
        install_dependencies 
        install_sqlite 
       elif [[ $cmd == "mysql" ]]; then 
        install_dependencies 
        install_mysql 
       elif [[ $cmd == "postgre" ]]; then 
        install_dependencies 
        install_postgre 
       elif [[ $cmd == "riak" ]]; then 
        printwarn "This feature will be supported in future versions" 
        exit 1 
       elif [[ $cmd == "pusher" ]]; then 
        install_dependencies 
        install_golang 
        install_cdrpusher 
        install_supervisord 
       elif [[ $cmd == "stats" ]]; then 
        install_dependencies 
        install_golang 
        install_cdrstats 
       else 
        HELP 
        exit 1 
       fi 
       isparsed=1 
       ;; 
      --) 
       shift 
       break 
       ;; 
     esac 
    done 
} 

私はそれが不履行されて伝えることができます-hフラグを指定して実行

main() { 
    . cdr_funcs 
    check_permissions 
    detect_os 
    parse_args 

    if [[ ${isparsed} == 1 ]]; then 
     INSTALL_COMPLETE_MESG 
     exit 0 
    fi 

    printerr "isparsed flag == 0" 
    HELP 
    exit 1 
} 

が露骨にこのことを示して)isparsedフラグがまだ設定されていないため、case文のその時点まで(== 0)と私は(私のメインのコンソールにエラーを出力します。

devbox cdr # ./cdr -h 
[DEBUG] [check_permissions]: Root Permissions Validated 
[DEBUG] [detect_os]: Starting OS Platform detection 
[DEBUG] [detect_os]: Checking OS Compatibility 
[DEBUG] [detect_os]: OS detected: [linux] (OS is supported) 
[DEBUG] [detect_os]: Finished OS detection 
[DEBUG] [parse_args]: parsing options 
[ERROR] [main]: isparsed flag == 0 

答えて

1

関数内で(parse_args)、"[email protected]"は、関数を呼び出す際の引数に展開され、囲むスクリプトでは展開されません。何もないので、getoptは引数を見ず、オプションを生成しません。

あなたは機能とスクリプトの引数を解析したい場合は、関数にそれらを提供する必要があります。

parse_args "[email protected]" 
+0

は、私はおかげでRICI、ことを逃した信じることができません! –