I'm trying to create a bash script and take in 2 options
- bePort and fePort
since this is working
while getopts ":a:b:" opt; do
case $opt in
a)
echo "-a was triggered!, Parameter: $OPTARG" >&2
;;
b)
echo "-b was triggered!, Parameter: $OPTARG" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
I run
bash test.sh -a aaa -b bbb
I got
-a was triggered!, Parameter: aaa
-b was triggered!, Parameter: bbb
So then, I've tried this
while getopts ":fe:be:" opt; do
case $opt in
fe)
echo "-fe was triggered!, Parameter: $OPTARG" >&2
;;
be)
echo "-be was triggered!, Parameter: $OPTARG" >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
But it doesn't seem to work.
I run
bash test.sh -fe 4444 -be 3333
I got nothing printing in my prompt.
I was expecting to see something like this
-be was triggered!, Parameter: 3333
-fe was triggered!, Parameter: 4444
How would one go about debugging this further?
-fe
as a single option, consider using the pattern described in BashFAQ #35 instead ofgetopts
to build your own parsing.grep --color=always
). Butgetopts
only handles single-letter options, so you need to use something else. Either that, or switch to single-letter options. Frankly, two-letter options are rather weird.