Issue
I'm trying to run a command that should find PHP files that contain "base64_decode" and/or "eval", echo the file name, print the top three lines, if the file contains more than 3 lines, also the bottom 3.
I have the following at the moment:
for file in $(find . -name "*.php" -exec grep -il "base64_decode\|eval" {} \;); do echo $file; head -n 3 $file; if [ wc -l < $file -gt 3 ]; then tail -n 3 $file fi; done | less
This returns the following error:
bash: syntax error near unexpected token `done'
Solution
I would to use the following
while read -r file
do
echo ==$file==
head -n 3 "$file"
[[ $(grep -c '' "$file") > 3 ]] && (echo ----last-3-lines--- ; tail -n 3 "$file")
done < <(find . -name \*.php -exec grep -il 'base64_decode\|eval' {} \+)
- Using
while
over thefor
is better, because the filenames could contain spaces. /probably not in this case, but anyway :)/ - using
grep -c '' "$file"
is sometimes better (when the last line in the file, doesn't contains the\n
character (thewc
counts the\n
characters in the file) - the
find
with the\+
instead of the\;
is more efficient
Answered By - clt60 Answer Checked By - Mildred Charles (WPSolving Admin)