This can be done in bash using pattern matching. You want to check if
- the substring is at the start of the string
- the substring is in the middle, or
- the substring is at the end
# pass the string, the substring, and the word separator
matches() {
[[ $1 == $2$3* ]] || [[ $1 == *$3$2$3* ]] || [[ $1 == *$3$2 ]]
}
msg="bbb. aaa.ccc. bbb.dddd. aaa.eee."
matches "$msg" "aaa." " " && echo y || echo n
matches "$msg" "bbb." " " && echo y || echo n
n
y
This works with dash, so it should work with ash too:
str_contains_word() {
sep=${3:-" "}
case "$1" in
"$2$sep"* | *"$sep$2$sep"* | *"$sep$2") return 0;;
*) return 1;;
esac
}
msg="bbb. aaa.ccc. bbb.dddd. aaa.eee."
for substr in aaa. bbb.; do
printf "%s: " "$substr"
if str_contains_word "$msg" "$substr"; then echo yes; else echo no; fi
done