[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
getopts state is not localized in shell functions
From: |
Wilhelm Mueller |
Subject: |
getopts state is not localized in shell functions |
Date: |
Fri, 7 Sep 2001 15:57:39 +0200 (MET DST) |
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 -D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -I. -I. -I./include -I./lib
-I/usr/local/share/include -O6
uname output: SunOS columba 5.7 Generic_106541-12 sun4u sparc SUNW,Ultra-1
Machine Type: sparc-sun-solaris2.6
Bash Version: 2.05
Patch Level: 0
Release Status: release
Description:
It is not possible to use getopts to parse the arguments of shell functions.
Using getopts with extra arguments after the variable name does not work either.
Though this behaviour is permitted by POSIX, it makes getopts almost useless
for functions.
Repeat-By:
Put the following lines into a file and run them through bash:
-- 8< ------------------------------------------------ >8 --
f () {
getopts a:b:c o
echo rc=$?
echo o=$o
echo OPTARG=$OPTARG
echo OPTIND=$OPTIND
echo ---------------------------------
}
echo Calling f -a 1 -c -b 2 -a 3
f -a 1 -c -b 2 -a 3
echo Calling f -a 11 -c -b 22 -a 33
f -a 11 -c -b 22 -a 33
echo Calling f -a 111 -c -b 222 -a 333
f -a 111 -c -b 222 -a 333
echo Calling getopts xyz o -x -y
getopts xyz o -x
echo rc=$?
echo o=$o
echo OPTARG=$OPTARG
echo OPTIND=$OPTIND
-- 8< ------------------------------------------------ >8 --
The result is:
Calling f -a 1 -c -b 2 -a 3
rc=0
o=a
OPTARG=1
OPTIND=3
---------------------------------
Calling f -a 11 -c -b 22 -a 33
rc=0
o=c
OPTARG=
OPTIND=4
---------------------------------
Calling f -a 111 -c -b 222 -a 333
rc=0
o=b
OPTARG=222
OPTIND=6
---------------------------------
Calling getopts xyz o -x -y
rc=1
o=?
OPTARG=222
OPTIND=2
but I think it should be (that's what ksh88 gives):
Calling f -a 1 -c -b 2 -a 3
rc=0
o=a
OPTARG=1
OPTIND=3
---------------------------------
Calling f -a 11 -c -b 22 -a 33
rc=0
o=a
OPTARG=11
OPTIND=3
---------------------------------
Calling f -a 111 -c -b 222 -a 333
rc=0
o=a
OPTARG=111
OPTIND=3
---------------------------------
Calling getopts xyz o -x -y
rc=0
o=x
OPTARG=
OPTIND=2
Fix:
Don't know
- getopts state is not localized in shell functions,
Wilhelm Mueller <=