开发者

In Unix, is it possible to give getops a range of values to expect?

开发者 https://www.devze.com 2023-01-21 15:57 出处:网络
Sorry if the title is confusing, but here\'s what I mean: If I have a script that can accept several parameters, I\'d use the getops command in order to more easily control script actions based on th

Sorry if the title is confusing, but here's what I mean:

If I have a script that can accept several parameters, I'd use the getops command in order to more easily control script actions based on the parameters passed. However, lets say one of these parameters can be any number from 5 - 9, or whatever. Is there a way to tell getops that any number passed as command to the script between 5 and 9 should be taken as a single user-command?

My code so far is something like:

#!/bin/sh
args=`getopt -o abc: -- "$@"`
eval set -- "$args"
echo "After getopt"
for i in $args
do
 case "$i" in
    -c) shift;echo "flag c set to $1";shift;;
    -a) shift;echo "flag a set";;
    -b) shift;echo "flag b set";;
done

I want to see if I can do something like:

#!/bin/sh
args=`getopt -o ab[0-9]c: -- "$@"`
eval set -- "$args"
echo "After getopt"
for i in $args
do
 case "$i" in
    -c) shi开发者_JS百科ft;echo "flag c set to $1";shift;;
    -a) shift;echo "flag a set";;
    -b) shift;echo "flag b set";;
    -[0-9]) shift; echo $i;;
done


No, at least not with the one I use (someone may have an enhanced one out there but it won't be standard anywhere).

For that particular case, it's probably okay to use:

args=`getopt -o ab0123456789c: -- "$@"`

although, for larger cases, that might be unwieldy.

Have you thought about not treating them as individual options? In other words, say they're debug levels for a logging procedure. Why could you not use:

args=`getopt -o abc:d: -- "$@"`

and specify them with progname -b -d4 instead of progname -b -4?

0

精彩评论

暂无评论...
验证码 换一张
取 消