[Top][All Lists]

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

getopts confuses lower and upper case options

From: address@hidden
Subject: getopts confuses lower and upper case options
Date: Thu, 20 Mar 2008 12:30:33 +1000 (EST)

Configuration Information [Automatically generated, do not change]:
Machine: mips
OS: irix6.5
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='mips' 
-DCONF_OSTYPE='irix6.5' -DCONF_MACHTYPE='mips-sgi-irix6.5' -DCONF_VENDOR='sgi' 
-DLOCALEDIR='/opt/local/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H  
 -I.  -I. -I./include -I./lib -I./lib/intl 
-I/usr/people/panjkova/projects/bash/build/lib/intl  -mabi=64 -mips4 -O2
uname output: IRIX64 pot 6.5 07010238 IP27
Machine Type: mips-sgi-irix6.5

Bash Version: 3.2
Patch Level: 0
Release Status: release

scripterror (source below) is a function that 
  puts the argument of option m in variable 'message'
  puts the argument of option M in variable `testText'

but as you can see, if i supply both options, the argument for -M has gone into 
the argument for -m instead:

# -bash 37 > scripterror -m messagetext -M testtext
Warning: in -bash: testtext

Also, if I supply only option -M, its argument again gets treated as if it is 
the argument of -m:

# -bash 38 > scripterror -M testtext
Warning: in -bash: testtext

Note however that if I only list one of -m or -M in the getopts list, they are 
handled with case sensitivity. (I.e., 
-m can't be used for -M)


load the function scripterror below, then issue  the test commands above to 
demonstrate the problem.
Look at the value of the variables message and testItem.

scripterror() { 
# Subversion keywords:
# $LastChangedDate: 2008-03-19 14:29:18 +1000 (Wed, 19 Mar 2008) $
# $LastChangedRevision$
# $LastChangedBy: panjkova $
# $HeadURL: 
svn://starship/CINRS/aussiegrass/users/panjkova/bin/bash_functions/scripterror $
    local verbosityRestoreCmd=$(localVerbositySave)
    set +x ; set +v
    local ThisFun=scripterror
    trap "eval $verbosityRestoreCmd" RETURN
    trap "echo Trapped an unforeseen error in scripterror with args " ERR
    usage="Usage: scripterror -m message -l location -s severity -e 
returnstatus -A emailTo \n
       \t -m error message\n
       \t -l string describing error location (e.g. script name, section, 
       \t -s severity : fatal - exit, warning - keep processing script\n
       \t -A email address : if given, then message sent to that address as 
       \t -e return status from last command, usually $? "

    local message="Unspecified error"
    local location="$0"
    local severity="Warning: "
    local emailTo=""
    local badoption=""
    local returnstatus=""

    #----# Save the getops variables before sweeping the CLAs. #----#
    local opt
    local saveOPTARG=${OPTARG:-""}
    local saveOPTIND=${OPTIND:-1}

    while getopts ":e:m:l:s:A:M:" opt ; do
        case $opt in
            e ) returnstatus=$OPTARG ;;
            m ) message=$OPTARG ;;
            l ) location=$OPTARG ;;
            s ) severity=$OPTARG ;;
            M ) testItem=$OPTARG ;;
            A ) emailTo=$OPTARG ;;
            \: ) echo "Argument missing from -$OPTARG option in function 
scripterror" ;;
            \? ) eval badoption=\$$((OPTIND - 1)) ;;
    shift $((OPTIND - 1 )) #--# Pushes the processed option/args out, leaving 
only unprocessed CLAs. #--#
    #----# Restore the getopts arguments. #----#

    echo testItem=$testItem
    echo message=$message

    #--subsequent args are ignored--#

    #----# Assemble the full message. #----#
    local fullMessage="$severity in $location: $message"
    if [ ! -z "$returnstatus" ] ; then
        fullMessage=$fullMessage":  Return status from last command was: 

    if [ ! -z "$badoption" ] ; then
        fullMessage=$fullMessage": Bad option to scripterror ($badoption) 
        echo -e $usage >&2

    #----# Output to stderr, and mail. #----#
    echo $fullMessage >&2

    if [ ! -z "$emailTo" ] ; then
        echo $fullMessage | mailx -s "$fullMessage" $emailTo

    if [ "$severity" = "FATAL" -o "$severity" = "fatal" ] ; then
        echo "Exiting!" >&2
        exit $returnstatus 
        return 0

The information in this email together with any attachments is
intended only for the person or entity to which it is addressed
and may contain confidential and/or privileged material.
Any form of review, disclosure, modification, distribution
and/or publication of this email message is prohibited, unless
as a necessary part of Departmental business.
If you have received this message in error, you are asked to
inform the sender as quickly as possible and delete this message
and any copies of this message from your computer and/or your
computer system network.

reply via email to

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