views:

20

answers:

1

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) shift;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
A: 

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?

Another possibility (for bash at least) is to use the sequence expression {0..9} (or {a..j} or others), which generates the sequence separated by spaces, to create your getopts string, something like:

args=$(getopt -o $(echo ab{0..9}c: | sed 's/ //g') -- "$@")

but that seems like more work to me than just including 0123456789.

paxdiablo