while getopts m: opt
do
case "$opt" in
(m) message="$OPTARG";;
(*) echo "Usage: $0 [-m 'message'] [chan ...]" >&2; exit 1;;
esac
done
shift $(($OPTIND-1))
for channel in "$@"
do
whatever -y because -m "$message" $channel
done
That should work. It leaves the message optional, and doesn't require you to specify any channels. You can add more options fairly easily (those that take an argument specified with a colon after the letter; those that don't take an argument without a colon). You can insist that $# is greater than 0 between the two loops (but the usage message has to change to reflect that). You can insist that a message is passed (but the usage message has to be changed for that, too). And so it goes on; your options are legion.
As noted in comments:
Note that ; is a command delimiter to the shell. You will have to write:
./application 'channels;message'
with quotes around the semicolon or a backslash before it. That's a not very orthodox usage; you might use a more idiomatic notation such as:
./application -m "this is a message" channel1 channel2 channel3
The script above implements this suggestion fairly accurately.
The only problem I see, is what if the message has " characters in it. ./application -m "this isn"t a message" channel1 etc...
You need to be cognizant of shell metacharacters, which include "'`$;(. For all those, you'll need to escape the culprit character:
./application -m 'He said, "Don'\''t do that!"' chan1
Generally, use single quotes around a message; where you have a single quote to write, type '\'' instead.
./application -m 'He said, "This is a message."' chan1
./application -m "This isn't a message." chan2
./application -m 'He said, "This isn'\''t a message", but they disagreed." chan3
;is a command delimiter to the shell. You will have to write:./application 'channels;message'with quotes around the semicolon or a backslash before it. That's a not very orthodox usage; you might use./application -m "this is a message" channel1 channel2 channel3more idiomatically.code./application -m "this isn"t a message" channel1 etc..."'`$;(. For all those, you'll need to escape the culprit character:./application -m 'He said, "Don'\''t do that!"' chan1, etc. Generally, use single quotes around a message; where you have a single quote to write, type'\''instead.