Denys Vlasenko | 4628945 | 2017-08-11 00:59:36 +0200 | [diff] [blame] | 1 | # Simple usage cases for getopts. |
| 2 | # |
| 3 | # OPTIND is either not touched at all (first loop with getopts, |
| 4 | # relying on shell startup init), or getopts state is reset |
| 5 | # before new loop with "unset OPTIND", "OPTIND=1" or "OPTIND=0". |
| 6 | # |
| 7 | # Each option is a separate argument (no "-abc"). This conceptually |
| 8 | # needs only $OPTIND to hold getopts state. |
| 9 | # |
| 10 | # We check that loop does not stop on unknown option (sets "?"), |
| 11 | # stops on _first_ non-option argument. |
| 12 | |
| 13 | echo "*** no OPTIND, optstring:'ab' args:-a -b c" |
| 14 | var=QWERTY |
| 15 | while getopts "ab" var -a -b c; do |
| 16 | echo "var:'$var' OPTIND:$OPTIND" |
| 17 | done |
| 18 | # unfortunately, "rc:0" is shown since while's overall exitcode is "success" |
| 19 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |
| 20 | |
| 21 | # Resetting behavior =1 |
| 22 | echo "*** OPTIND=1, optstring:'ab' args:-a -b c" |
| 23 | OPTIND=1 |
| 24 | while getopts "ab" var -a -b c; do |
| 25 | echo "var:'$var' OPTIND:$OPTIND" |
| 26 | done |
| 27 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |
| 28 | |
| 29 | # Resetting behavior =0 |
| 30 | echo "*** OPTIND=0, optstring:'ab' args:-a -b c" |
| 31 | OPTIND=0 |
| 32 | while getopts "ab" var -a -b c; do |
| 33 | echo "var:'$var' OPTIND:$OPTIND" |
| 34 | done |
| 35 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |
| 36 | |
| 37 | # Resetting behavior "unset" |
| 38 | echo "*** unset OPTIND, optstring:'ab' args:-a -b c" |
| 39 | unset OPTIND |
| 40 | while getopts "ab" var -a -b c; do |
| 41 | echo "var:'$var' OPTIND:$OPTIND" |
| 42 | done |
| 43 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |
| 44 | |
| 45 | # What is the final exitcode? |
| 46 | echo "*** optstring:'ab' args:-a -b c" |
| 47 | unset OPTIND |
| 48 | getopts "ab" var -a -b c; echo "1 rc:$? var:'$var' OPTIND:$OPTIND" |
| 49 | getopts "ab" var -a -b c; echo "2 rc:$? var:'$var' OPTIND:$OPTIND" |
| 50 | getopts "ab" var -a -b c; echo "3 rc:$? var:'$var' OPTIND:$OPTIND" |
| 51 | |
| 52 | # Where would it stop? c or -c? |
| 53 | echo "*** unset OPTIND, optstring:'ab' args:-a c -c -b d" |
| 54 | unset OPTIND |
| 55 | while getopts "ab" var -a c -c -b d; do |
| 56 | echo "var:'$var' OPTIND:$OPTIND" |
| 57 | done |
| 58 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |
| 59 | |
| 60 | # What happens on unknown option? |
| 61 | echo "*** unset OPTIND, optstring:'ab' args:-a -c -b d" |
| 62 | unset OPTIND |
| 63 | while getopts "ab" var -a -c -b d; do |
| 64 | echo "var:'$var' OPTIND:$OPTIND" |
| 65 | done |
| 66 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |
| 67 | |
| 68 | # ORTERR=0 suppresses error message? |
| 69 | echo "*** unset OPTIND, OPTERR=0, optstring:'ab' args:-a -c -b d" |
| 70 | unset OPTIND |
| 71 | OPTERR=0 |
| 72 | while getopts "ab" var -a -c -b d; do |
| 73 | echo "var:'$var' OPTIND:$OPTIND" |
| 74 | done |
| 75 | echo "exited: rc:$? var:'$var' OPTIND:$OPTIND" |