Bash script (using find and grep)

I'm trying to make a simple search script but cannot get it right. The script should search for keywords inside files. Then return the file paths in a variable. (Each file path separated with \n).

#!/bin/bash

SEARCHQUERY="searchword1 searchword2 searchword3";

for WORD in $SEARCHQUERY
do
    GREPINPUT=$GREPINPUT" | grep --ignore-case --files-with-matches -e '$WORD'";
done

FINDFILES=$(find . -maxdepth 2 -name \*.c -type f $($GREPINPUT));

I usually search inside a file with:

cd /home/users/; grep -R "keyword" * | gawk -F: '{ print $1 }' >> output.txt

This will output the path of the found files to output.txt

Play around with that in your script, should be fun!

OK, but I am trying to insert a concatenated string inside another string and evaluate it. I have been playing with this for hours but cannot get it right.

Any help please?

How about:

#!/bin/bash
SEARCHQUERY="searchword1|searchword2|searchword3"
FINDFILES=$(find . -maxdepth 2 -name \*.c -type f |xargs egrep -lie "$SEARCHQUERY")

-or-

FINDFILES=$(find . -maxdepth 2 -name \*.c -type f -exec egrep -lie "$SEARCHQUERY" {} \; )

That will output searches that match each keyword. But what I am trying to do is match files that match ALL the keywords.

I'm still playing around with no results.

SEARCHQUERY="searchword1.*searchword2.*searchword3"

An AND function, but not on the same line and not necessarily in that order? OK, I think this might work:

#!/bin/bash
Q1="searchword1"; Q2="searchword2"; Q3="searchword3";
FINDFILES=$(find . -maxdepth 2 -name \*.c -type f |xargs grep -li "$Q1"|xargs grep -li "$Q2"|xargs grep -li "$Q3")

So you were close but you need xargs to make grep in the file content rather than the file name.