Using getopts to process long and short command line options

后端 未结 30 1614
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 22:52

I wish to have long and short forms of command line options invoked using my shell script.

I know that getopts can be used, but like in Perl, I have not

30条回答
  •  失恋的感觉
    2020-11-21 23:08

    I kind of solved this way:

    # A string with command options
    options=$@
    
    # An array with all the arguments
    arguments=($options)
    
    # Loop index
    index=0
    
    for argument in $options
      do
        # Incrementing index
        index=`expr $index + 1`
    
        # The conditions
        case $argument in
          -a) echo "key $argument value ${arguments[index]}" ;;
          -abc) echo "key $argument value ${arguments[index]}" ;;
        esac
      done
    
    exit;
    

    Am I being dumb or something? getopt and getopts are so confusing.

提交回复
热议问题