Debian Bug report logs - #47849
bash: bizarre behavior of getopt in shell functions

version graph

Package: pdksh; Maintainer for pdksh is Robert Luberda <robert@debian.org>; Source for pdksh is src:mksh.

Reported by: Zack Weinberg <zack@codesourcery.com>

Date: Wed, 20 Oct 1999 04:33:04 UTC

Severity: normal

Merged with 47848

Found in version 5.2.14-1

Done: Thorsten Glaser <tg@mirbsd.de>

Bug is archived. No further changes may be made.

Forwarded to michael@cs.mun.ca

Toggle useless messages

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to debian-bugs-dist@lists.debian.org, Guy Maor <maor@debian.org>:
Bug#47849; Package bash. Full text and rfc822 format available.

Acknowledgement sent to Zack Weinberg <zack@bitmover.com>:
New Bug report received and forwarded. Copy sent to Guy Maor <maor@debian.org>. Full text and rfc822 format available.

Message #5 received at submit@bugs.debian.org (full text, mbox):

From: Zack Weinberg <zack@bitmover.com>
To: submit@bugs.debian.org
Subject: bash: bizarre behavior of getopt in shell functions
Date: Tue, 19 Oct 1999 21:29:50 -0700
Package: bash
Version: 2.02.1-1.8
Severity: normal

Consider this shell script:

--
fun_a () {
	while getopts ksgq opt
	do	echo $opt
	done
}

fun_b () {
	while getopts a: opt
	do	echo $opt $OPTARG
	done
	shift `expr $OPTIND - 1`
	echo "$@"
	fun_a -k -sg
}

fun_b
echo
fun_b -a blah
echo
fun_b -a blah bar
--

I expected it to print this:
$ att-ksh test.sh

k
s
g

a blah

k
s
g

a blah
bar
k
s
g
$

but I got this:
$ bash test.sh

k
s
g



bar
$

Please fix.

zw

-- System Information
Debian Release: potato
Kernel Version: Linux zack 2.2.13pre15 #1 Mon Oct 4 22:12:49 PDT 1999 i686 unknown

Versions of the packages bash depends on:
ii  libc6           2.1.2-5        GNU C Library: Shared libraries and timezone
ii  libncurses4     4.2-3.4        Shared libraries for terminal handling


Information forwarded to debian-bugs-dist@lists.debian.org, Guy Maor <maor@debian.org>:
Bug#47849; Package bash. Full text and rfc822 format available.

Acknowledgement sent to Matthias Klose <doko@tango.isdn.cs.tu-berlin.de>:
Extra info received and forwarded to list. Copy sent to Guy Maor <maor@debian.org>. Full text and rfc822 format available.

Message #10 received at 47849@bugs.debian.org (full text, mbox):

From: Matthias Klose <doko@tango.isdn.cs.tu-berlin.de>
To: 47849@bugs.debian.org, Zack Weinberg <zack@bitmover.com>
Cc: chet@po.cwru.edu
Subject: your debian bug report #47849
Date: Sat, 20 Nov 1999 18:35:42 +0100 (MET)
From the upstream author:

47849	You *must* reset OPTIND to 1 before reusing getopts


Noted your statement that Bug has been forwarded to chet@po.cwru.edu. Request was from Matthias Klose <doko@tango.isdn.cs.tu-berlin.de> to control@bugs.debian.org. Full text and rfc822 format available.

Forwarded-to-address changed from chet@po.cwru.edu to chet@po.cwru.edu. Request was from Matthias Klose <doko@cs.tu-berlin.de> to control@bugs.debian.org. Full text and rfc822 format available.

Information forwarded to debian-bugs-dist@lists.debian.org, Guy Maor <maor@debian.org>:
Bug#47849; Package bash. Full text and rfc822 format available.

Acknowledgement sent to Zack Weinberg <zack@bitmover.com>:
Extra info received and forwarded to list. Copy sent to Guy Maor <maor@debian.org>. Full text and rfc822 format available.

Message #19 received at 47849@bugs.debian.org (full text, mbox):

From: Zack Weinberg <zack@bitmover.com>
To: Matthias Klose <doko@tango.isdn.cs.tu-berlin.de>
Cc: 47849@bugs.debian.org, chet@po.cwru.edu
Subject: Re: your debian bug report #47849
Date: Sun, 21 Nov 1999 10:44:07 -0800
Matthias Klose wrote:
> From the upstream author:
> 
> 47849	You *must* reset OPTIND to 1 before reusing getopts

I'm aware that the standard leaves the behavior undefined if you
don't.  I still want it to work correctly.  If OPTIND and its friends
are not specific to each shell function (like $@ already must be; the
infrastructure is there) then there are a number of natural constructs
which cannot be used.  For example, one would like to be able to call
a function from _inside_ a getopts loop without having to worry about
whether that function uses getopts internally.

zw


Information forwarded to debian-bugs-dist@lists.debian.org, Guy Maor <maor@debian.org>:
Bug#47849; Package bash. Full text and rfc822 format available.

Acknowledgement sent to chet@po.CWRU.Edu:
Extra info received and forwarded to list. Copy sent to Guy Maor <maor@debian.org>. Full text and rfc822 format available.

Message #24 received at 47849@bugs.debian.org (full text, mbox):

From: Chet Ramey <chet@nike.ins.cwru.edu>
To: zack@bitmover.com
Cc: doko@tango.isdn.cs.tu-berlin.de, 47849@bugs.debian.org, chet@po.cwru.edu
Subject: Re: your debian bug report #47849
Date: Mon, 22 Nov 1999 07:26:33 -0500
> Matthias Klose wrote:
> > From the upstream author:
> > 
> > 47849	You *must* reset OPTIND to 1 before reusing getopts
> 
> I'm aware that the standard leaves the behavior undefined if you
> don't.  I still want it to work correctly.  If OPTIND and its friends
> are not specific to each shell function (like $@ already must be; the
> infrastructure is there) then there are a number of natural constructs
> which cannot be used.  For example, one would like to be able to call
> a function from _inside_ a getopts loop without having to worry about
> whether that function uses getopts internally.

I'm not going to introduce more weirdly-scoped variables.  The shell
doesn't need any more special cases.  Correctly-written functions can
use getopts without problems and without affecting the calling
environment by simply declaring OPTIND as local.

If you can provide me with an example where declaring OPTIND as local
in a function produces erroroneous results, I will fix that.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet)

Chet Ramey, CWRU    chet@po.CWRU.Edu    http://cnswww.cns.cwru.edu/~chet/


Information forwarded to debian-bugs-dist@lists.debian.org, Guy Maor <maor@debian.org>:
Bug#47849; Package bash. Full text and rfc822 format available.

Acknowledgement sent to Zack Weinberg <zack@bitmover.com>:
Extra info received and forwarded to list. Copy sent to Guy Maor <maor@debian.org>. Full text and rfc822 format available.

Message #29 received at 47849@bugs.debian.org (full text, mbox):

From: Zack Weinberg <zack@bitmover.com>
To: chet@po.cwru.edu
Cc: doko@tango.isdn.cs.tu-berlin.de, 47849@bugs.debian.org
Subject: Re: your debian bug report #47849
Date: Mon, 22 Nov 1999 11:10:36 -0800
Chet Ramey wrote:
> > Matthias Klose wrote:
> > > From the upstream author:
> > > 
> > > 47849	You *must* reset OPTIND to 1 before reusing getopts
> > 
> > I'm aware that the standard leaves the behavior undefined if you
> > don't.  I still want it to work correctly.  If OPTIND and its friends
> > are not specific to each shell function (like $@ already must be; the
> > infrastructure is there) then there are a number of natural constructs
> > which cannot be used.  For example, one would like to be able to call
> > a function from _inside_ a getopts loop without having to worry about
> > whether that function uses getopts internally.
> 
> I'm not going to introduce more weirdly-scoped variables.  The shell
> doesn't need any more special cases.  Correctly-written functions can
> use getopts without problems and without affecting the calling
> environment by simply declaring OPTIND as local.

Localizing OPTIND is not portable: bash does do the right thing, but both
ksh and ash ignore the request.  I'm willing to call that a bug in ksh
and ash, and close this bug report.

zw


Bug reassigned from package `bash' to `ksh'. Request was from Matthias Klose <doko@cs.tu-berlin.de> to control@bugs.debian.org. Full text and rfc822 format available.

Bug reassigned from package `ksh' to `pdksh'. Request was from Ben Harris <bjh21@cam.ac.uk> to control@bugs.debian.org. Full text and rfc822 format available.

Removed annotation that Bug had been forwarded to chet@po.cwru.edu. Request was from Zack Weinberg <zack@wolery.cumb.org> to control@bugs.debian.org. Full text and rfc822 format available.

Bug closed, ack sent to submitter - they'd better know why ! Request was from Zack Weinberg <zack@wolery.cumb.org> to control@bugs.debian.org. Full text and rfc822 format available.

Bug reopened, originator set to Zack Weinberg <zack@wolery.cumb.org>. Request was from Zack Weinberg <zack@wolery.cumb.org> to control@bugs.debian.org. Full text and rfc822 format available.

Noted your statement that Bug has been forwarded to michael@cs.mun.ca. Request was from Zack Weinberg <zack@wolery.cumb.org> to control@bugs.debian.org. Full text and rfc822 format available.

Merged 47848 47849. Request was from Zack Weinberg <zack@wolery.cumb.org> to control@bugs.debian.org. Full text and rfc822 format available.

Disconnected #47848 from all other report(s). Request was from Zack Weinberg <zack@codesourcery.com> to control@bugs.debian.org. Full text and rfc822 format available.

Bug closed, send any further explanations to Zack Weinberg <zack@wolery.cumb.org> Request was from Zack Weinberg <zack@codesourcery.com> to control@bugs.debian.org. Full text and rfc822 format available.

Bug reopened, originator set to Zack Weinberg <zack@codesourcery.com>. Request was from Zack Weinberg <zack@codesourcery.com> to control@bugs.debian.org. Full text and rfc822 format available.

Merged 47848 47849. Request was from Zack Weinberg <zack@codesourcery.com> to control@bugs.debian.org. Full text and rfc822 format available.

Bug archived. Request was from Debbugs Internal Request <owner@bugs.debian.org> to internal_control@bugs.debian.org. (Thu, 26 Jul 2012 07:32:26 GMT) Full text and rfc822 format available.

Send a report that this bug log contains spam.


Debian bug tracking system administrator <owner@bugs.debian.org>. Last modified: Sat Apr 19 08:12:25 2014; Machine Name: beach.debian.org

Debian Bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.