[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: "find" ends with exit code "0" although exec command returned an err
From: |
Bernhard Voelker |
Subject: |
Re: "find" ends with exit code "0" although exec command returned an error |
Date: |
Mon, 5 Oct 2015 08:54:15 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 |
On 10/04/2015 06:46 PM, Thomas D. wrote:
> Hi,
>
> for me it looks like "find" returns an invalid exit code when "-exec"
> returns an error:
>
> $ mkdir /tmp/find-test
> $ touch /tmp/find-test/scripts{1..10}.sh
> $ touch /tmp/find-test/text{1..10}.txt
> $ find /tmp/find-test -name '*.sh' -exec chmod 755 \; \
> && echo "find succeed" || echo "find failed"
>
> Yes, I am missing the "{}" but that's how I encounter the bug.
> Output:
>
>> chmod: missing operand after ‘755’
>> Try 'chmod --help' for more information.
>> chmod: missing operand after ‘755’
>> Try 'chmod --help' for more information.
>> chmod: missing operand after ‘755’
>> Try 'chmod --help' for more information.
>> chmod: missing operand after ‘755’
>> Try 'chmod --help' for more information.
>> chmod: missing operand after ‘755’
>> Try 'chmod --help' for more information.
>> chmod: missing operand after ‘755’
>> Try 'chmod --help' for more information.
>> chmod: missing operand after ‘755’
>> Try 'chmod --help' for more information.
>> chmod: missing operand after ‘755’
>> Try 'chmod --help' for more information.
>> chmod: missing operand after ‘755’
>> Try 'chmod --help' for more information.
>> chmod: missing operand after ‘755’
>> Try 'chmod --help' for more information.
>> find succeed
>
> I expected "find failed" ("find" should have ended with non-zero exit code).
>
> And yes, "chmod 755" returns an error:
>
> $ chmod 755
> chmod: missing operand after ‘755’
> Try 'chmod --help' for more information.
> $ echo $?
> 1
>
>
>
> $ find -version
> find (GNU findutils) 4.5.14
> Packaged by Gentoo (4.5.14-r1)
> Copyright (C) 2014 Free Software Foundation, Inc.
> License GPLv3+: GNU GPL version 3 or later
> <http://gnu.org/licenses/gpl.html>.
> This is free software: you are free to change and redistribute it.
> There is NO WARRANTY, to the extent permitted by law.
Thanks for the report, however, although the result may be surprising a bit,
I think it is mandated by POSIX:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html
If the primary expression is punctuated by a <semicolon>, the utility
utility_name shall be invoked once for each pathname and the primary
shall evaluate as true if the utility returns a zero value as exit
status.
[...]
If the primary expression is punctuated by a <plus-sign>, [...]. If any
invocation returns a non-zero value as exit status, the find utility shall
return a non-zero exit status.
You see, there's that subtle difference between "find -exec ... \;" and
"find -exec ... +" which can be demonstrated like this:
$ touch f
$ find f -exec false '{}' \; ; echo $?
0
$ find f -exec false '{}' + ; echo $?
1
That behaviour is somehow documented in the manpage, yet it could be
more explicit - especially the difference between the return value of
an expression and the exit code of find in general. E.g. the following
demonstrates that by having a 2nd -exec expression which whitnesses the
return value of the first one:
$ find /bin/true -exec env '{}' \; -exec echo 'file {}: 1st exec returned
true' \; ; echo $?
file /bin/true: 1st exec returned true
0
$ find /bin/false -exec env '{}' \; -exec echo 'file {}: 1st exec returned
true' \; ; echo $?
0
Of course, the return value of the expression does not affect the
exit value in the above example due to the "-exec ... \;", while it
would have with the "-exec ... +" syntax.
Have a nice day,
Berny