Trying to compare input to a file containing alert words,
read MYINPUT
alertWords=( `cat "AlertWordList" `)
for X in "${alertWords[@]}"
do
# the wildcards in my expression do not work
if [[ $MYINPUT =~ *$X* ]]
then
echo "#1 matched"
else
echo "#1 nope"
fi
done
I’d avoid
=~here because as FatalError points out, it will interpret$Xas a regular expression and this can lead to surprising bugs (especially since it’s an extended regular expression, so it has more special characters than standard grep syntax).Instead, you can just use
==because bash treats the RHS of==as a globbing pattern:I’ve also removed a use of cat in your
alertWordsassignment, as it keeps the file reading inside the shell instead of spawning another process to do it.