I have a task to parse a files in subdirectory for certain conditions and then perform something, so I have a code which works fine and looks like this:
cat FILENAME | grep -qe 'condition'; \
test $? -ne 0 && echo -ne '\n add condition to file \n'>>FILENAME
But, when I try to use this in xargs statement I have some problems:
git grep -e "condition" | awk -F : 'BEGIN {} { print $1 } END {}'| sort| uniq |\
xargs -I {} -i bash -c "cp {} {}.updated; echo -ne 'Processing: {}\n'; \
cat {}.updated | grep -qe 'condition'; \
test $? -ne 0 && echo -ne '\n add condition\n' >>{}.updated; \
mv {} {}.backup; mv {}.updated {}; echo -ne ' End\n';"
Problem that test $? -ne 0 inside xargs always evaluates as true, despites the actual grep result.
Try changing $? to \$?
Bash is probably evaluating $? prematurely