getopts in a subshell

Hello,

I've a little problem with one of my ksh scripts and I manage to narrow it to the script here:

#!/bin/ksh
writeLog()
{
  paramHandle="unknown"
  OPTIND=1
  while getopts :i: option $*
  do
    case $option in
      i)  paramHandle=${OPTARG} ;;
    esac
  done
  echo ${paramHandle}
}

logHandle="known"
writeLog -i ${logHandle}
( writeLog -i ${logHandle} )
writeLog -i ${logHandle}

The result of this script is

known
known
unknown

whereas I'm waiting for

known
known
known

Could someone explain me why running the function in a subshell result in a malfunctionning of getopts ?

Thanks in adavance

This is working as expected for me:

$ echo $KSH_VERSION
Version JM 93t+ 2010-03-05
$ ./little_program 
known
known
known

Also works OK with

  • ksh version M-11/16/88,
  • pdksh version 5.2.14 99/07/13.2
  • bash 4.1.9(3)-release, 3.2.51(24)-release and 3.2.16(1)-release
1 Like

Thanks ...

$ ksh --version
  version         sh (AT&T Research) 93s+ 2008-01-31

I tried with bash and it works ...

$ bash --version
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)

I found those bugreports that seems to address the same issue:
bugzilla.redhat.com/show_bug.cgi?id=443889
bugzilla.redhat.com/show_bug.cgi?id=541654

This may be corrected with two ksh bug-fixes
rhn.redhat.com/errata/RHBA-2009-1256.html
rhn.redhat.com/errata/RHBA-2010-0234.html

I'l try to see with my sysadmin if he can update ksh.
If not, as I'm tied with ksh, I'll be doomed to find a workaround :mad:

(Sorry I couldn't use the full url as I don't have enough posts on this board :wall:)