bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

colon not placed in NAME when required argument is not found


From: gk
Subject: colon not placed in NAME when required argument is not found
Date: Wed, 30 Apr 2003 15:50:53 -0700

info bash says:
> If a required argument is not found, and `getopts' is not silent,
     a question mark (`?') is placed in NAME, `OPTARG' is unset, and a
     diagnostic message is printed.  If `getopts' is silent, then a
     colon (`:') is placed in NAME and `OPTARG' is set to the option
     character found.

This does not appear to be true in GNU bash, version 2.05b.0(1)-release.

Below is my test script 'getopt.test' and the results.

#!/bin/bash
# PURPOSE:
#   test getopts
HR="---"

# silent mode
while getopts ":a:b" Option
do
        case $Option in
                [ab] )
                        echo "option=$Option";;
                ? )
                        echo "illegal option=$OPTARG";;
                : )
                        echo "missing arg to option: $OPTARG"; exit 1;;
        esac
        echo OPTARG="$OPTARG";
        echo OPTIND="$OPTIND" >&2;
        echo $HR >&2;
done
shift $(($OPTIND - 1))
# EOF

[greg@p3 junk]$ ./getopt.test -a
illegal option=a
OPTARG=a
OPTIND=2
---

NOTE: missing argument should have caused $Option to be set to ':' instead of '?'
QUESTION: Does getopts think we are not in silent mode here?

[greg@p3 junk]$ ./getopt.test -a -b -c
option=a
OPTARG=-b
OPTIND=3
---
illegal option=c
OPTARG=c
OPTIND=4
---

NOTE: '-b' is considered an 'argument' to option -a

QUESTIONS:
* How is it possible for getopts to ever detect a missing argument?
* I thought getopts used the leading '-' to distinguish between options and non-option arguments; if so, then why does it think '-b' is the argument to -a?


- Greg Keraunen
http://www.xmake.org





reply via email to

[Prev in Thread] Current Thread [Next in Thread]