1

I would like to output the names of files for which a command succeeds, when I expect it will fail,while suppressing the warnings. I'd prefer a one-liner.

Something along the lines of:

find xml/malformed-xml -type f -iname '*.xml' -exec if xmllint --noout --dtdvalid check-xml.dtd {} 2>/dev/null; then echo "Should have failed for {}"; fi \;

or

find xml/malformed-xml -type f -iname '*.xml' -print0 | xargs -0 -n 1 -i if xmllint --noout --dtdvalid check-xml.dtd {} 2>/dev/null ; then echo "Should have failed for {}"; fi

and so far, I am not getting much love. Suggestions?

Al Pacifico
  • 592
  • 3
  • 14

3 Answers3

1

Try this:

find xml/malformed-xml -type f -iname '*.xml' -exec xmllint --noout --dtdvalid check-xml.dtd  {} \; -print 2>/dev/null

Explanation: in find, arguments are implicitly separated by an -and. So -exec ... {} \; -print is really -exec ... {} \; -print. Now because of the short-circuiting logic of -and, the second part will only be executed if the first one succeeds.

The reason why both your sugestions fail is that if ... then ... else ... is not a program, it's a bash builtin, so you cannot call it from find -exec or xargs. Both of those programs can execute only actual programs.

redneb
  • 16,685
  • 4
  • 31
  • 50
0

i think this might help you:

Is there a TRY CATCH command in Bash

with || and && you can substitute a try-catch kind of behaviour

Community
  • 1
  • 1
Gewure
  • 1,092
  • 15
  • 27
0

Try by calling a subshell with "-exec sh -c ... "

find xml/malformed-xml -type f -iname '*.xml' -exec sh -c "if xmllint --noout --dtdvalid check-xml.dtd {} 2>/dev/null; then echo Should have failed for {}; fi" \;
mpurg
  • 191
  • 6