Discussion:
[KSH'88] passing args to getopts when getopts is embedded in functions
(too old to reply)
Andrew Falanga
2003-08-23 02:24:40 UTC
Permalink
hello,

I hope that the subject line is descriptive enough. What I'm trying
to do is basically this, parse command line arguments passed to the
script by getopts while getopts is nested two functions deep. The
script is called with something similar to:

script -n arg_here -f "arg here too"

Parsing the options is easy if the call to getopts is NOT embedded in
functions. Also, if I don't use multi-word arguments getopts has no
trouble regardless of where it sets. Below is a stripped down version
of the script:

#! /usr/bin/ksh

main () {
ParseArgs $*
}

ParseArgs () {
while getopts d:t: FOO
do case in $FOO
d) do stuff ;;
t) do more stuff ;;
esac
done
}

main $*

I'm sure all of you can see the problem already. I've tried various
things like calling ParseArgs, and main, like so, main "$*" ParseArgs
"$*" or ParseArgs "$@" and so forth to no avail.

Using the -x option to korn I was able to determine that my problem is
how the arguments are being passed, or at least seen, by the
functions. Before the args are processed by the functions, they are
seen properly in the global area, if you will, of the script.
However, after being passed to the functions, the integrity of the
arguments is lost and, depending on how I pass them, they either
become one singe argument, or every word becomes and argument.

(This might be second nature to all of you, but it seems odd to me
that these functions should be passed arguments like this when their
not defined as taking arguments.)

Any help would be appreciated. I'm sure the answer is simple, but
it's escaping me. How do I prevent this from happening?

Andy
Bill Marcum
2003-08-24 20:59:10 UTC
Permalink
On 22 Aug 2003 19:24:40 -0700, Andrew Falanga
Post by Andrew Falanga
hello,
I hope that the subject line is descriptive enough. What I'm trying
to do is basically this, parse command line arguments passed to the
script by getopts while getopts is nested two functions deep. The
script -n arg_here -f "arg here too"
Parsing the options is easy if the call to getopts is NOT embedded in
functions. Also, if I don't use multi-word arguments getopts has no
trouble regardless of where it sets. Below is a stripped down version
#! /usr/bin/ksh
main () {
ParseArgs $*
}
Change $* to "$@"

Loading...