Reputation: 21
Part of my script takes in all parameters and looks for any flag options. I'm trying to save these into my array, but it doesn't seem to be matching. I can't figure it out, what am I missing?
#!/bin/bash
ALL_PARAMS=( "$@" )
ARGUMENTS=()
OPTIONS=()
for i in ${ALL_PARAMS[@]}
do
if [ $i == ^- ]
then
ARGUMENTS+=($i)
else
OPTIONS+=($i)
fi
done
echo ${ARGUMENTS[@]}
echo ${OPTIONS[@]}
Upvotes: 1
Views: 1724
Reputation: 42007
The test
command ([
) does not do Regex matching, the bash
keyword [[
does.
You need:
[[ $i =~ ^- ]]
Also note that, you need Regex operator =~
instead of equality operator ==
.
Upvotes: 2