20

My shell script is quite simple, as the following:

  while getopts "abc:" flag; do
         echo "$flag" $OPTIND $OPTARG
  done

And I do some testing as the following:

Blank@Blank-PC:~/lab/shell/getopts_go$ sh foo.sh -abc CCC Blank
a 1
b 1
c 3 CCC

Blank@Blank-PC:~/lab/shell/getopts_go$ sh foo.sh -a -b -c CCC Blank
a 2
b 3
c 5 CCC

Blank@Blank-PC:~/lab/shell/getopts_go$ sh foo.sh -ab -c CCC Blank
a 1
b 2
c 4 CCC

Blank@Blank-PC:~/lab/shell/getopts_go$ sh foo.sh -a -bc CCC Blank
a 2
b 2
c 4 CCC

I cannot understand how OPTIND works with different command line invocation, I am confused by the output.

Can you help to figure out the mechanism of computing OPTIND?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Blank
  • 343
  • 1
  • 3
  • 8

1 Answers1

25

According to man getopts, OPTIND is the index of the next argument to be processed (starting index is 1). Hence,

In sh foo.sh -abc CCC Blank arg1 is -abc, so after a we are still parsing arg1 when next is b (a 1). Same is true when next is c, we are still in arg1 (b 1). When we are at c, since c needs an argument (CCC) the OPTIND is 3 (arg2 is CCC and we skip it).

In sh foo.sh -a -b -c CCC Blank, arg1 is a, arg2 is b, arg3 is c, and arg4 is CCC. So we get a 2, b 3, c 5.

In sh foo.sh -ab -c CCC Blank args are (1:-ab, 2: -c, 3: CCC and 4: Blank). So we get: a 1, b 2, c 4.

In sh foo.sh -a -bc CCC Blank args are (1: -a, 2: -bc, 3: CCC, 4: Blank) and we get a 2, b 2, c 4.

perreal
  • 94,503
  • 21
  • 155
  • 181
  • 1
    Really appreciating your explanation, it troubles me a lot. It turns out to be that I am confused by the meaning of next argument. – Blank Jan 10 '13 at 06:05