Unix Shell - Getopts wildcard

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > June 2006 > Getopts wildcard





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Getopts wildcard
Dan

2006-06-08, 1:25 pm

Is there a way to give getopts a wildcard or a range of options like
[a-z]? I'd like it to continue processing arguments even if it finds
one it doesn't know. For example here's a script called adhoc.sh:

#!/usr/bin/ksh

while getopts :a:b:c: OPT ; do
case ${OPT} in
a) A=$OPTARG;;
b) B=$OPTARG;;
c) C=$OPTARG;;
?) echo "WARNING! Unknown environment option '${OPT}'";;
esac
echo "$OPT:$OPTARG"
done

When I run it:

adhoc.sh -a testA -d testD -c testC
a:testA
WARNING! Unknown environment option '?'
?:d

I'd like it to bypass the -d and continue with the -c. Thanks!
Dan

Stephane Chazelas

2006-06-08, 1:25 pm

On 8 Jun 2006 05:37:14 -0700, Dan wrote:
> Is there a way to give getopts a wildcard or a range of options like
> [a-z]? I'd like it to continue processing arguments even if it finds
> one it doesn't know. For example here's a script called adhoc.sh:
>
> #!/usr/bin/ksh
>
> while getopts :a:b:c: OPT ; do
> case ${OPT} in
> a) A=$OPTARG;;
> b) B=$OPTARG;;
> c) C=$OPTARG;;
> ?) echo "WARNING! Unknown environment option '${OPT}'";;
> esac
> echo "$OPT:$OPTARG"
> done
>
> When I run it:
>
> adhoc.sh -a testA -d testD -c testC
> a:testA
> WARNING! Unknown environment option '?'
> ?:d
>
> I'd like it to bypass the -d and continue with the -c. Thanks!

[...]

How would getopts know whether -d takes an argument or not.

What's wrong with:

while getopts :a:b:c:d:e:f:g:h:i: OPT ; do
case ${OPT} in
a) A=$OPTARG;;
b) B=$OPTARG;;
c) C=$OPTARG;;
'?') echo >&2 "ERROR! '$OPTARG' unknown and unsupported"
# but doesn't take arguments
exit 1;;
?) echo >&2 "WARNING! Unknown environment option '${OPT}'";;
esac
echo "$OPT:$OPTARG"
done


--
Stephane
Dan

2006-06-09, 7:23 am

The script I'm working with is a template that sets generic variables.
It's invoked by other scirpts that could use any number of other
options. I could do :a:b:c:...:x:y:z: but that seems kind of clumsy.
I was hoping for a more elegant solution.

Stephane Chazelas wrote:
> What's wrong with:
>
> while getopts :a:b:c:d:e:f:g:h:i: OPT ; do


Stephane Chazelas

2006-06-09, 7:23 am

On 9 Jun 2006 04:39:58 -0700, Dan wrote:
> The script I'm working with is a template that sets generic variables.
> It's invoked by other scirpts that could use any number of other
> options. I could do :a:b:c:...:x:y:z: but that seems kind of clumsy.
> I was hoping for a more elegant solution.


You can still make the range by yourself:

getopts_range() { # args: first last [take-arg]
LC_ALL=C awk -v first="$1" -v last="$2" -v opt="${3:+:}" '
BEGIN {
for (i = 1; i < 256; i++) {
c = sprintf("%c", i)
if (c >= first && c <= last && c != ":")
result = result c opt
}
print result
}'
}
[vbcol=seagreen]
>
> Stephane Chazelas wrote:

Then:

while getopts ":$(getopts_range a z " OPT; do
....

--
Stephane
Dan

2006-06-09, 1:23 pm

Wow - I never would have thought of that. Thanks!

Stephane Chazelas wrote:
> You can still make the range by yourself:
>
> getopts_range() { # args: first last [take-arg]
> LC_ALL=C awk -v first="$1" -v last="$2" -v opt="${3:+:}" '
> BEGIN {
> for (i = 1; i < 256; i++) {
> c = sprintf("%c", i)
> if (c >= first && c <= last && c != ":")
> result = result c opt
> }
> print result
> }'
> }
>
> Then:
>
> while getopts ":$(getopts_range a z " OPT; do


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com