bug-bash
[Top][All Lists]
Advanced

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

Error when using getopts in a function


From: Jeff Spires
Subject: Error when using getopts in a function
Date: Tue, 26 Dec 2000 17:31:47 -0500

Configuration Information [Automatically generated, do not change]:
Machine: sparc
OS: solaris2.6
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='sparc' 
-DCONF_OSTYPE='solaris2.6'
-DCONF_MACHTYPE='sparc-sun-solaris2.6' -DCONF_VENDOR='sun' -DSHELL 
-DHAVE_CONFIG_H 
-I/user/adc/shr/ut/export/sparc-sun-solaris2.6/t/zlib/1.1.3/include
-I/user/adc/shr/ut/export/sparc-sun-solaris2.6/t/libjpeg/6b/include
-I/user/adc/shr/ut/export/sparc-sun-solaris2.6/t/libpng/1.0.5/include
-I/user/adc/shr/ut/export/sparc-sun-solaris2.6/t/tiff/3.5.4/include
-I/user/adc/shr/ut/export/sparc-sun-solaris2.6/t/freetype/1.3.1/include
-I/user/adc/shr/ut/export/sparc-sun-solaris2.6/t/libungif/4.1.0b1/include -I.  
-I. -I./lib
-I/user/adc/shr/ut/export/sparc-sun-solaris2.6/t/bash/2.03/include -g -O2
uname output: SunOS cairo 5.6 Generic_105181-10 sun4u sparc SUNW,Ultra-5_10
Machine Type: sparc-sun-solaris2.6

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

Description:
        The getopts shell builtin uses the OPTIND variable to keep
        track of the next argument to be processed. When all the
        options have been processed, OPTIND is generally used to
        shift the options out of the argument list. Bash does not
        reset OPTIND when a function exits, so calling the same
        function multiple times will result in the argument list
        being parsed incorrectly (e.g. options are skipped or
        arguments are incorrectly shifted out of the argument list).

Repeat-By:
        Use the following function:
        
        function foo {
          let hide=0

          while getopts ":h" OPTION; do
            case $OPTION in
              h )   let hide=1;;
            esac
          done
          shift $((OPTIND - 1))

          if (( hide )); then
            echo "i'm not going to show you arg 1"
          else
            echo "arg 1 = :$1:"
          fi
        }
        
        Run this several times from the command line with 'foo bar'.
        As you would expect, the function reports the first argument
        correctly. Now run this several times with 'foo -q bar'. You
        would expect the function to hide the first argument every
        time, but after running the function with the -q option once,
        it fails to work on subsequent runs. Check OPTIND. It is
        set to 2 which causes getopts to skip over the -q, and shift
        removes the option from the command line.

Fix:
        I don't know how ksh and zsh handle the OPTIND variable, but
        both shells seem to work.

-- 
Jeff Spires                                      Phone: (978) 247-8068
Applied Micro Circuits Corp.                     Fax:   (978) 623-0024




reply via email to

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