Accessing shell script arguments by index

前端 未结 2 609
借酒劲吻你
借酒劲吻你 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
    

提交回复
热议问题