When you run into a problem like this, it's a good idea to remove as much code as possible. If we just take that one line with the multiple grep
statements, we can first verify that the current expression doesn't work:
$ echo passed | ((grep -q -m2 passed || grep -q -v "not passed") || echo failed
$ echo not passed | ((grep -q -m2 passed || grep -q -v "not passed") || echo failed
We can see that neither of these commands produces at any output.
Let's think carefully about the logic:
The ||
operator means "if the first command doesn't succeed, run the second command". So in both cases, the first grep succeeds (because both passed
and not passed
contain the phrase passed
). This means the second grep will never run, and it means that since the first command was successful, the entire grep ... || grep ...
command will be successful, and that means the final echo $f
will never run.
I was trying to think of a clever way to solve this, but it seems simplest if we make use of a temporary file:
OIFS="$IFS"
IFS=$'\n'
tmpfile=$(mktemp docXXXXXX)
trap "rm -f $tmpfile" EXIT
for f in $(find . -wholename '*_done/(*Report*.docx' |grep -v appendix)
do
docx2txt "$f" - | head -2 > $tmpfile
if grep -q passed $tmpfile && ! grep -q 'not passed' $tmpfile; then
echo $f >> failed
fi
done
IFS="$OIFS"