Hi,
I'm using bash and ksh93 compatible derivatives.
In a recent getopts experience, I found myself spending far too much
time on this little problem. I hope someone can help...
So here's the deal.
I want to build have a command line interface that accepts either zero,
one, or many parameters of the same type. Assume the script sends
e-mails to a recipient list. I tried to write the code to handle this, but it
did not work:
commandname -r "user1@tld user2@tld user3@tld"
This did not work because the getopts seemed to parse strings using
space as delimiter regardless of the existence of the surrounding double
or single quotes. This resulted in OPTARGS containing only the first
element of the list. (e.g. user1@tld)
So, I changed the UI to be such that any number (0, 1, or more) of the
-r options can be on the command line.
commandname -r user1@tld -r user2@tld -r user3@tld
Now, my getopts loop has this form:
while getopts "r:" opt; do
case $opt in
r) ONE_STRING="${OPTARG}"
;;
esac
STRING_LIST="${STRING_LIST} ${ONE_STRING}"
done
It successfully grabs every single occurrence of the -r option from the
command line. I created the variable, STRING_LIST, to "accumulate" the
strings.
Is there a better way to do this style of zero, one, or more options of
the same type?
Is there a better way to build a list that does not create anomalies such
as a space character at the beginning or end. (My example above has a
single space character as the 1st character of STRING_LIST, due to fact
it is an empty variable the first time it is referenced on the RHS of
the = sign.)
#####
BTW, I am unable change tools or languages so if you want to suggest
that I use Perl or Ruby or even Java, then please include a example code
(for others to see) otherwise why bother acting like a smarty pants if
you can't display a nice example?