Accessing shell script arguments by index

前端 未结 2 603
借酒劲吻你
借酒劲吻你 2020-12-10 21:53

I\'m sure this is a no-brainer when you\'re into shell programming. Unfortunately I\'m not and I\'m having a pretty hard time ...

I need to verify arguments passed t

相关标签:
2条回答
  • 2020-12-10 22:20

    There is no method to access next argument in for.

    1. How about to rewriting your script to use getopt?

    2. If you don't like getopt, try to rewrite your script using shift:

    
    while [ -n "$1" ]
    do
      str="$1"
      minus=${str:0:1}
      if [ "$str" == "-o" ]
      then
        shift
        par="$1"
    #  ...
      elif [ "$minus" == "-" ]
      then
        # append element into array
        myArray[${#myArray[@]}]="$str"
      fi
    
      shift
    done
    
    0 讨论(0)
  • 2020-12-10 22:44

    In bash, you can use indirect parameter expansion to access an arbitrary positional parameter.

    $ set a b c
    $ paramIndex=2
    $ echo $2
    b
    $ echo ${!paramIndex}
    b
    
    0 讨论(0)
提交回复
热议问题