I am trying to write a shell program that will search my current directory (say, my folder containing C code), read all files for the keywords “printf” or “fprintf”, and append the include statement to the file if it isn’t already done.
I have tried to write the search portion already (for now, all it does is search files and print the list of matching files), but it is not working. Included below is my code. What am I doing wrong?

EDIT: New code.
#!/bin/sh
#processes files ending in .c and appends statements if necessary
#search for files that meet criteria
for file in $( find . -type f )
do
echo $file
if grep -q printf "$file"
then
echo "File $file contains command"
fi
done
To execute commands in a subshell you need
$( command ). Notice the$before the parenthesis.You don’t need to store the list of files in a temporary variable, you can directly use
And with
you are not searching the file content but the file name (in my example all the files which name contains “somestring”)
To grep the content of the files:
Note that if you match
printfit will also matchfprintf(as it containsprintf)If you want to search just files ending with
.cyou can use the-nameoptionUse the
-type foption to list only files.In any case check if your
grephas the-roption to search recursively