1

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

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
  • 2
    what is the question?? – Anubhab Jan 02 '14 at 12:03
  • Can you elaborate in little more detail like what exactly you want to do with above script? – Yogesh Jan 02 '14 at 12:06
  • okay if I understand you correctly you are trying to bind both 1 and 2 with `-A` option, you can pass 2 with another switch like `./test.sh -A 1 -B 2` and then parse them. Here is a helpful link => http://stackoverflow.com/questions/16483119/example-of-how-to-use-getopt-in-bash – Ashish Gaur Jan 02 '14 at 12:11

1 Answers1

3

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.

Igor Chubin
  • 61,765
  • 13
  • 122
  • 144