How get next to next argument in linux shell scrip

2019-08-05 05:15发布

问题:

I have wrote simple shell script test.sh as follows:

while getopts ":A:" OPTION
do 
   case $OPTION in
   A)
      echo $OPTARG
   ?)
      echo "no option"

  esac
done

And executed the scripts as follows

$ ./test.sh -A 1 2

Now if got argument 1 by $OPTARG but how can i access the second argument ( 2 in this case)?

Regards Jayesh

回答1:

There are several options.

(1) You can use shift and take $1

 while -n "$1"
 do  
  # do something with $1
  shift
 done

(2) You can iterate through the args:

 for i
 do
    # do something with $i
 done

There are other alternatives also.