Below is the script to find specific array of different words "bots,spyware,virus" in file, if file exists.
#!/bin/bash
#strings to find in file
NAME[0]="bots"
NAME[1]="spyware"
NAME[2]="virus"
#location of file
LOGS=/home/testing.txt
#define function to grep any of the above mentioned array strings in file
func(){
if `grep "${NAME[*]}" $LOGS`; then
echo "CRITICAL: ${NAME[*]}"
else
echo "errors not found"
exit
fi
}
#file exist or not exist
if [ -f $LOGS ]; then
echo " File Found"
#call functions
func
modified
else
echo "File Not Found"
exit
But grep "${NAME[*]}" $LOGS does not work. It shows below error:
grep: virus: No such file or directory
grep: bots: No such file or directory
set -xto turn on debugging trace of shell script (set +xto turn off). And you'll discover you need to convert your arr into a search target that looks likegrep 'a|b|c' file. And great 2nd Q, but we only need to see thefunc()code, so learn to target your Qs to the non-working code. Good luck.