How get next to next argument in linux shell scrip

2019-08-05 04:22发布

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条回答
太酷不给撩
2楼-- · 2019-08-05 05:07

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.

查看更多
登录 后发表回答