【发布时间】:2016-07-30 23:47:26
【问题描述】:
根据man find
-false 始终为 false。
这个选项的目的是什么? 什么时候使用?
【问题讨论】:
-
也许stackoverflow.com/q/21525727/3220113 解释了什么。
-
在this 答案中添加了示例。
根据man find
-false 始终为 false。
这个选项的目的是什么? 什么时候使用?
【问题讨论】:
false 和 true 选项将与 find 命令中的复杂测试表达式一起使用。
find 每次处理文件时都会计算表达式。表达式可以包含以下任何类型的原色:
选项
影响整体操作而不是特定文件的处理;
测试
根据文件的属性返回真或假值;
动作
有副作用并返回真或假值;和
运营商
连接其他参数并影响它们何时以及是否被评估。
操作员通过测试和操作构建复杂的表达式。运算符按优先级降序排列:
( 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 根据优先级规则从左到右计算表达式,查找以每个文件名为根的目录树,直到知道结果(左侧为 false 代表 '-and',true 代表 ' -or'),此时 find 会移动到下一个文件名。
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 作为逻辑运算符,如果它的任何参数为真,则计算结果为真,大多数编程语言停止计算并返回真(这称为短路)。为了防止这种情况,您可以强制每个参数返回 false,从而评估所有 or 的术语。
【讨论】:
find版本(由于限制无法更新)。它不支持上述测试选项。 :(
-false - 感觉有点老套。如果不这样做,您真的不能拥有多个 if 语句吗?
find 命令,是不可能让if 控制的。因为finddoes not have 是if 选项。