Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 1 | # Open Group Base Specifications Issue 7: |
| 2 | # """ |
| 3 | # If an unknown option is met, VAR shall be set to "?". In this case, |
| 4 | # if the first character in optstring is ":", OPTARG shall be set |
| 5 | # to the option character found, but no output shall be written |
| 6 | # to standard error; otherwise, the shell variable OPTARG shall be |
| 7 | # unset and a diagnostic message shall be written to standard error." |
| 8 | # ... |
| 9 | # If an option-argument is missing: |
| 10 | # If the first character of optstring is ":", VAR shall be set to ":" |
| 11 | # and OPTARG shall be set to the option character found. |
| 12 | # """ |
| 13 | |
Denys Vlasenko | 007ce9f | 2017-08-13 02:59:00 +0200 | [diff] [blame] | 14 | ( |
| 15 | |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 16 | echo "*** optstring:':ac' args:-a -b -c" |
| 17 | getopts ":ac" var -a -b -c; echo "1 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" |
| 18 | getopts ":ac" var -a -b -c; echo "2 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" |
| 19 | getopts ":ac" var -a -b -c; echo "3 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" |
| 20 | getopts ":ac" var -a -b -c; echo "4 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" |
| 21 | # Previous line should result in "rc:1", which is normally treated |
| 22 | # in getopts loops as exit condition. |
| 23 | # Nevertheless, let's verify that calling it yet another time doesn't do |
| 24 | # anything weird: |
| 25 | getopts ":ac" var -a -b -c; echo "5 rc:$? var:'$var' OPTIND:$OPTIND OPTARG:'$OPTARG'" |
Denys Vlasenko | 007ce9f | 2017-08-13 02:59:00 +0200 | [diff] [blame] | 26 | |
| 27 | ) 2>&1 \ |
| 28 | | sed -e 's/ unrecognized option: / invalid option -- /' \ |
| 29 | -e 's/ illegal option -- / invalid option -- /' \ |