find 的 "false" 选项的目的是什么?

What is the purpose of find's "false" option?

根据man find

-false Always false.

此选项的用途是什么? 什么时候使用?

falsetrue 选项将与 find 命令中的复杂测试表达式一起使用。


gnu documentation:

find 在每次处理文件时计算表达式。表达式可以包含以下任何类型的原色:

  1. 选项

    影响整体操作而不是特定文件的处理;

  2. 测试

    return 真值或假值,取决于文件的属性;

  3. 操作数

    有副作用和return一个真值或假值;和

  4. 运算符

    连接其他参数并影响它们何时以及是否被评估。

Combining Primaries With Operators

操作员根据测试和操作构建复杂的表达式。运算符按优先级递减顺序排列为:

( expr )
    Force precedence. True if expr is true.
! expr
-not expr
    True if expr is false. In some shells, it is necessary to protect 
    the ‘!’ from shell interpretation by quoting it.
expr1 expr2
expr1 -a expr2
expr1 -and expr2
    And; expr2 is not evaluated if expr1 is false.
expr1 -o expr2
expr1 -or expr2
    Or; expr2 is not evaluated if expr1 is true.
expr1 , expr2
    List; both expr1 and expr2 are always evaluated. 
    True if expr2 is true. The value of expr1 is discarded. 
    This operator lets you do multiple independent operations on one 
    traversal, without depending on whether other operations succeeded. 
    The two operations expr1 and expr2 are not always fully independent,
     since expr1 might have side effects like touching or deleting files,
     or it might use ‘-prune’ which would also affect expr2. 
— Test: -true  
    Always true. 
— Test: -false
    Always false.     

find 根据优先规则从左到右计算表达式,搜索以每个文件名为根的目录树,直到知道结果(对于“-and”,左侧为 false,为 true对于“-or”),此时查找移动到下一个文件名。

示例:source question

find . \( \! -user xx -exec chown -- xx '{}' + -false \) -o    \
\( \! -group root -exec chgrp -- root '{}' + \) -o \
\( ! -perm 700 -exec chmod -- 700 '{}' + -exec false \; \)

解释:(作者perreal

-o 的 false 谓词计算结果为 false,此处用于防止短路。

if user is not xx make it xx
if group is not root, make it root
if not all permissions are set for the owner, grant all permissions.

每个命令由 -o 分隔并以 false 终止,以便它们全部应用于每个项目。

上述命令中false的目的是什么?

or 作为逻辑运算符,如果它的任何参数为真,则计算结果为真,大多数编程语言停止计算并且 return 为真(称为短路)。为了防止这种情况,您可以强制每个参数为 return false 并因此评估所有 or 的术语。