If you are the find command, you have a difficult job to do when setting your exit code. That's because you have 2 types inputs (forgetting options for the moment) with two types of jobs:
- The path(s) to the files you want to find. You likely would not, for example, want to exit with an error if you did
find /mnt/log/storage/place -type f -mtime +7 -print
. Likely there ARE files in your log storage which are younger than 7 days, so it is not an error to skip past them- that is your intent. It WOULD be an error if you could not get into the path /mnt/log/storage/place
at all.
- The expression which controls find's actions. Elements in the expression are either true or false. For the purposes of
-exec
, it is true if the command it's running returns 0, false if it returns nonzero. To change the behavior of -exec
would render it unique among all the other expression elements in find, and likely break a lot of existing scripts out there in the world.
So, to another solution to your question: you could do something which is similar to LatinSuD's while
loop- in that you check on your command's status from a variable- but much simpler:
errorout=$(find /tmp -type f \( -exec bash -c '/tmp/doit 1>/tmp/stdoutfile' \; -o -quit \) 2>&1 )
If the script /tmp/doit
is not executable or produces output to stderr, the errorout variable will get filled. Test errorout and react like:
[ -n "$errorout" ] || { echo 'Oh no- an error from find!'; /do/something/else; }
For a one-liner, you could do this:
errorout=$(find /tmp -type f \( -exec bash -c '/bin/false || { echo "error" 1>&2; exit 1; }' \; -o -quit \) 2>&1 )
Replace /bin/false
with a command of your choosing. You can execute a command list like this:
errorout=$(find /tmp -type f \( -exec bash -c '{ command1 && command2 && command3; } || { echo "error" 1>&2; exit 1; }' \; -o -quit \) 2>&1 )
And if you didn't want to exit immediately upon the first failure of a command in your list:
errorout=$(find /tmp -type f \( -exec bash -c '{ command1; command2; command3; } || { echo "error" 1>&2; exit 1; }' \; -o -quit \) 2>&1 )
...don't forget the semicolons before the closing braces in the command strings.
find
is not suitable for this. You cankill $PPID
in the subshell, but i don't know how to do the return thing. Maybe you can combinefind
with somebash
code, like this stackoverflow.com/questions/1116992/…