Reputation: 331
I have a strange situation. For different parameters I always get the same result
function test
{
while getopts 'c:S:T:' opt ; do
case "$opt" in
c) STATEMENT=$OPTARG;;
S) SCHEMA=$OPTARG;;
T) TABLE=$OPTARG;;
esac
done
echo "$STATEMENT, $SCHEMA, $TABLE"
}
test -c CREATE -S schema1 -T tabela1
test -c TRUNCATE -S schema2 -T tabela2
test -c DROP -S schema3 -T tabela3
Result:
CREATE, schema1, tabela1
CREATE, schema1, tabela1
CREATE, schema1, tabela1
What is failed in my script?
Upvotes: 2
Views: 71
Reputation: 242218
In bash, you need to localize the $OPTIND variable.
function test () {
local OPTIND
Otherwise it's global and the next call to getopts
returns false (i.e. all arguments processed). Consider localizing the other variables, too, if they're not used outside of the function.
You can also just set it to zero.
Upvotes: 2