Note: This is a beta release of Red Hat Bugzilla 5.0. The data contained within is a snapshot of the live data so any changes you make will not be reflected in the production Bugzilla. Also email is disabled so feel free to test any aspect of the site that you want. File any problems you find or give feedback here.
Bug 4768 - getopts (built-in bash function) problem
Summary: getopts (built-in bash function) problem
Keywords:
Status: CLOSED WORKSFORME
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: bash
Version: 6.0
Hardware: i386
OS: Linux
medium
medium
Target Milestone: ---
Assignee: Bernhard Rosenkraenzer
QA Contact:
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 1999-08-29 11:34 UTC by giulioo
Modified: 2008-05-01 15:37 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 1999-08-31 02:55:53 UTC


Attachments (Terms of Use)

Description giulioo 1999-08-29 11:34:12 UTC
I have rh60 (with GNU bash, version 1.14.7(1)) + kernel
2.2.12.

It seems there's a problem with getopts built-in bash
function, when called from within another script which uses
getopts itself: the second getopts is not evaluated if the
first is.

Here are two shell scripts, a1 and a2, which show the
problem (bug).
If you run a2 directly all is ok, if a2 is called by a1 a
weird thing happens (the a2's getopts is not evaluated):

=== a1 ===
animal=dog
while getopts a par
do
        case $par in
                a) animal=cat ;;
        esac
done
echo $animal

./a2 -b
=========

=== a2 ===
color=white
while getopts b par
do
        case $par in
                b) color=black ;;
        esac
done
echo $color
=========

$ ./a2
white
$ ./a2 -b
black
$ ./a1
dog
black
$ ./a1 -a
cat
white    <== WRONG, should be "black".

I've a Caldera1.3 box with "GNU bash, version 1.14.7(2)"
which works as expected; I took the bash from Caldera, put
it on rh60 (with libc5 installed) and it has the same
problem, so the real problem should be in a place
different from bash, but I cannot understand where.

I found out the the problem can be resolved by putting
unset OPTIND
at the beginning of a2, but this is not a viable solutions
if you have many scripts.

Comment 1 Preston Brown 1999-08-29 15:32:59 UTC
We suggest that you run this script under bash2, which displays the
correct behaviour.  The bash (v1) behavior may or may not be easily
correctable; this bug has been passed along to the bash (v1) package
maintainer.

Comment 2 Cristian Gafton 1999-08-31 02:55:59 UTC
You need to put the #!/bin/bash magic at the beginning of each
script.  If I do that and I have the following scripts mde mode 755
the things are working okay:

a1:

#!/bin/bash
animal=dog
while getopts a par
do
        case $par in
                a) animal=cat ;;
        esac
done
echo $animal

./a2 -b



a2:
#!/bin/bash
color=white
while getopts b par
do
        case $par in
                b) color=black ;;
        esac
done
echo $color

Comment 3 Jeff Johnson 2000-03-18 20:57:59 UTC
*** Bug 9917 has been marked as a duplicate of this bug. ***


Note You need to log in before you can comment on or make changes to this bug.