Bash – Find files and check if they were executed successfully

bashfind

I would like to count successfully executed files, but I can't get it to go.

This is what I have:

successfulScripts=0
allScripts=0
commandline=$(find . -name "*.sh" -exec '{}' \;)
    if [ $commandline -gt 0 ]; then
successfulScripts=$(($successfulScripts+1))
allScripts=$(($allScripts+1))
    else
allScripts=$(($allScripts+1))
    fi
echo "$successfulScripts out of $allScripts scripts were executed successfully"

I don't mind if content of a script is also shown, but is there any way to despise it?

Best Answer

You could just let find use the value returned by the script. eg

find . -name "*.sh" -exec '{}' \; -print

will execute the script and then -print will only print the names of those scripts which return 0. The output will be interleaved with the output of the scripts, so maybe you just want to redirect the script output elsewhere. On the other hand, if you want to print a pretty summary at the end, maybe you're looking for something like:

{ find . -name '*.sh' -exec sh -c 'if "$1" >&3; then
    echo success; else echo fail; fi' _ {} \; \
    | awk '/success/{s++} /fail/{e++}
    END {printf "%d successes out of %d\n", s, s + e}
 '; } 3>&1
Related Question