如何发现“修剪”行为?

根据GNU的手册页find-print行动默认使用和

抑制默认的操作-print-delete-exec-execdir-ok-okdir-fls-fprint-fprintf-ls-print-printf

因此,-prune行动仍应意味着-print行动。

实际上,确实如此。

$ tree .
.
├── dir/
│   └── file2
└── file1

$ find . -name dir #0
./dir

$ find . -name dir -prune #1
./dir #printed as expected

$ find . -name dir -prune -or -name file1 #2
./file1
./dir #printed as expected

但是,有时会-prune禁止默认设置-print

$ find . -name dir -prune -or -name file1 -print #3 #last -print is only added to the above example
./file1

$ find . -name dir -prune -or -print #4
.
./file1

我如何理解这个矛盾?


我的理解

#1

  1. file1不满意,-name dir所以被跳过。

  2. dir满足-name dir修剪条件,dir并添加到TODO列表中。

  3. -print还适用dir于TODO列表。

#2

  1. file1满足,-name file1因此已添加到TODO列表中。

  2. #1-2相同

  3. -print另外施加到dirfile1在TODO列表。

#3

  1. #2-1相同

  2. #2-2相同

  3. -print应用于file1TODO列表中。

  4. -print 应该另外应用,dir因为-prune它不会抑制-print(但这是不正确的。为什么?)

#4

  1. file1 已添加到TODO列表。

  2. #3-2相同

  3. #3-3相同

  4. #3-4相同

(实际上,中没有TODO列表find。请参阅此注释标准。)


补充

正如oguz ismail的答案(现已删除)中指出的那样,我的问题与无关-prune但是,这个问题没有解决。

让我们考虑一下-name A -o -name B -print这分为两个表达式:-name A-name B -print

我的理解:第一个表达式-name A没有作用。所以-print应该暗示。换句话说,-name A -o -name B -print应解释为-name A -print -o -name B -print

实际行为:-name A -o -name B -print是一种复合表达式。-print在该化合物中的表达。因此,没有其他-print暗示。

模棱两可,但我相信我的解释会更加自然,因为在这种情况下,每个文件满足-name A-name B -print满足(两个表达式从不同时满足)

如此评论该评论所写,OP中的Supplement部分中概述的我的问题来自于GNU手册页中的歧义,find而POSIX具有更好的解释。我发现这是真的。

POSIX

(如果没有表达存在,-print。应使用作为表达否则,)如果给定的表达不包含任何基色的-exec-ok-print,给定的表达应被有效地替换为:

( given_expression ) -print

并且很自然地解释given_expression一个包含一个或多个子表达式的复合表达式,因为它用括号括起来。(如果这given_expression指向单个子表达式,则括号肯定是多余的。)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章