1

I use this command for test my html pages for errors:

find . -iname '*html' -type f -print -exec /usr/local/bin/tidy -q -e {} \;

And I want to get exit code > 0 if any errors was founded by tidy.

find always return me 0.

Is there any way to sums exit code from tidy, and return it from find or any other wrapper script?

2 Answers2

4

You could try something like this:

find . -iname '*html' -type f \( -exec tidy -q -e {} \; -o -print \)

This would only print out filenames for which tidy exited with an error code. You could use -fprint to collect the filenames in a file:

find . -iname '*html' -type f \( -exec tidy -q -e {} \; -o -fprint files_with_errors \)

These constructs take advantage of the fact that -exec is a boolean expression that returns true or false depending on the success of the command; the -o flag is a boolean OR, so this reads:

find all entries that match *html AND that are files AND ( for which tidy returns true OR print the filename)

larsks
  • 44,886
  • 15
  • 124
  • 183
2

Try this:

#!/bin/bash

NUM_FAILS=0

for FILE in $(find . -iname '*.html' -type f -print); do
  /usr/local/bin/tidy -q -e ${FILE}

  if [ $? -ne 0 ]; then
    ((NUM_FAILS++))
  fi

done

if [ ${NUM_FAILS} -gt 0 ]; then
  echo -e "There were ${NUM_FAILS} failed files."
  exit 1
fi

exit 0

Explanation: You'll want to loop through the results of the find command, run tidy, then increment a counter if an error is found (I assume tidy will generate a non-zero return code on error).

Once you've looped through the files, if there are any errors, you can exit 1, and it's always good practice to explicitly exit 0 if things completed successfully.

Craig Watson
  • 9,670
  • 3
  • 33
  • 47