bug-bash
[Top][All Lists]
Advanced

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

getopts


From: Sid Elmer
Subject: getopts
Date: Wed, 22 Jan 2003 13:15:12 -0800
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.8) Gecko/20020204

From: sid
To: address@hidden
Subject: [50 character or so descriptive subject here (for reference)]

Configuration Information [Automatically generated, do not change]:
Machine: i386
OS: linux
Compiler: gcc -I/usr/src/packages/BUILD/bash-2.05
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='i386' -DCONF_OSTYPE='linux' -DCONF_MACHTYPE='i386-suse-linux' -DCONF_VENDOR='suse' -DSHELL -DHAVE_CONFIG_H -D_FILE_OFFSET_BITS=64 -I. -I/usr/include -I. -I./include -I./lib -I/usr/include -O2 -march=i486 -mcpu=i686 -D_GNU_SOURCE -Wall -pipe uname output: Linux DN800c9806 2.4.18-4GB #2 Wed Jul 31 14:57:36 PDT 2002 i686 unknown
Machine Type: i386-suse-linux

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

Description:
       [Detailed description of the problem, suggestion, or complaint.]
I am trying to write a shell function in bash. I want the function to be as useful as possible so I use "getopts" to process options to the function. Below in the Repeat-By Section, I have included a sample script with an example of a function I tried to write. And I also included the output when I run the script so hopefully it is clear what is going wrong.

I wrote a function, counting, which takes options to set the beginning number, ending number, and stride, then prints out the sequence of numbers. The script calls this function multiple times. The first time it is called with no options, so the default number sequence is printed out. Then the script enters a loop where the "counting" function is called three more times. The first time it is called, the function works perfectly. However, subsequent calls to the "counting" function fail to give the expected results. It appears that the option list is not processed by getopts, but rather, the default sequence is printed. Finally, and most perplexing of all, the script makes one last call to the "counting" function, after the for loop. The output of this call shows that the option for the stride variable is processed, but the other two options are ignored. Is this a bug with getopts?? Or am I forgetting a call to reinitialize getopts somewhere?? I've looked through the bash users' guide and found a reference that says OPTIND needs to be manually reset in order for getopts to work properly. I've tried to manually reset OPTIND by placing the following line directly after each call to the "counting" function:

       let OPTIND=1

but I get the same output. Did I not reset OPTIND properly? If not, could someone give me an example of how to properly reset OPTIND? Or, if I've completely missed the boat, could someone please instruct me what I need to do to get the function working properly? Thank you very much.

Sid Elmer
address@hidden


Repeat-By:
       [Describe the sequence of events that causes the problem
       to occur.]


----------------------------
--------  script  ----------
----------------------------
bash> cat tmp.sh
#!/bin/bash

function counting ()
{
       #set defaults
       local -i begin=1
       local -i end=10
       local -i skip=1

       echo "before getopts:  OPTIND = $OPTIND"
       while getopts ':b:e:s:' opt; do
               case $opt in
                       b)      begin=$OPTARG
                               echo "middle: OPTIND = $OPTIND";;
                       e)      end=$OPTARG
                               echo "middle: OPTIND = $OPTIND";;
                       s)      skip=$OPTARG
                               echo "middle: OPTIND = $OPTIND";;
                       \?)     echo "ERROR: $OPTARG is not a valid option"
                               exit 1;;
               esac
       done
       shift $(( $OPTIND-1 ))
       echo "after getopts:  OPTIND = $OPTIND"

       for ((j=$begin;j<=$end;j+=$skip)); do
               echo "$j"
       done
}

echo "defaults"
counting
echo
echo
echo "loop"
for ((i=10;i<13;i++)); do
       echo "i=$i"
       counting -b $i -e $(( $i+5 ))
       echo
done
echo
counting -b 50 -e 75 -s 5

-------------------------------
------  output  ---------------
-------------------------------
bash> ./tmp.sh
defaults
before getopts:  OPTIND = 1
after getopts:  OPTIND = 1
1
2
3
4
5
6
7
8
9
10


loop
i=10
before getopts:  OPTIND = 1
middle: OPTIND = 3
middle: OPTIND = 5
after getopts:  OPTIND = 5
10
11
12
13
14
15

i=11
before getopts:  OPTIND = 5
after getopts:  OPTIND = 5
1
2
3
4
5
6
7
8
9
10

i=12
before getopts:  OPTIND = 5
after getopts:  OPTIND = 5
1
2
3
4
5
6
7
8
9
10


before getopts:  OPTIND = 5
middle: OPTIND = 7
after getopts:  OPTIND = 7
1
6


Fix:
       [Description of how to fix the problem.  If you don't know a
       fix for the problem, don't include this section.]





reply via email to

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