A shell is an environment from which to call tools, not a tool to manipulate text. The guys who invented shell also invented awk for shell to call to manipulate text. The awk script included below will work clearly, robustly, efficiently, and portably using any awk in any shell on every UNIX box:
$ cat tst.sh
#!/bin/env bash
checkWordsInSentence() {
awk -v wordsStr="${array[*]}" -v sentenceStr="$text" 'BEGIN {
split(sentenceStr,words,/[^[:alpha:]]+/)
for (i in words) {
sentence[words[i]]
}
split(wordsStr,words)
for (i in words) {
word = words[i]
totWords++
if (word in sentence) {
status[word] = "present"
numPresent++
}
else {
status[word] = "absent"
}
}
if (totWords == numPresent) {
printf "All %d words (%s) present\n", totWords, wordsStr
}
else {
printf "%d of %d words (%s) present\n", numPresent, totWords, wordsStr
for (word in status) {
print word, status[word]
}
}
}'
}
array=(Jack Jessy Harold Ronald Boston Naomi)
text=" Jack is maried with Jessy, they have 3 children Ronald Boston and Naomi and Harold is the last one "
checkWordsInSentence
echo '----'
array=(Jack Jessy Harold Bob Ronald Boston Naomi Karen)
text=" Jack is maried with Jessy, they have 3 children Ronald Boston and Naomi and Harold is the last one "
checkWordsInSentence
.
$ ./tst.sh
All 6 words (Jack Jessy Harold Ronald Boston Naomi) present
----
6 of 8 words (Jack Jessy Harold Bob Ronald Boston Naomi Karen) present
Karen absent
Ronald present
Bob absent
Jack present
Boston present
Naomi present
Jessy present
Harold present
bash, or any shell. The purpose of a shell is to run other programs, not process data.bash, don't write them inbash.