我一直在處理man手冊,不知道find如何執行測試順序。find中的評估順序
請注意Solaris的find命令沒有單獨的「測試」和「動作」,而只是測試表達式的片段。
在這種情況下,對於像
find . -type d -mount -ctime +5 -prune -exec 'rm {}' \;
命令行能夠保證所有的 「式d」 和 「-ctime 5」 的前評估 「-exec 'RM {}' \;」因此,只有正確的文件被刪除?
我一直在處理man手冊,不知道find如何執行測試順序。find中的評估順序
請注意Solaris的find命令沒有單獨的「測試」和「動作」,而只是測試表達式的片段。
在這種情況下,對於像
find . -type d -mount -ctime +5 -prune -exec 'rm {}' \;
命令行能夠保證所有的 「式d」 和 「-ctime 5」 的前評估 「-exec 'RM {}' \;」因此,只有正確的文件被刪除?
說明
的find
的各種實現傾向於看漲的operands
不同的名字在man
頁。然而,遵循posix
標準的任何find
執行的一般執行將是相同的。從posix find man page
採取
find [-H | -L] path ... [operand_expression ...]
手冊頁的報價在哪裏了每組兩個感人expressions
(意-operand (Argument)
),不具有明確的運營商分離expressions
,有一個隱-a
(AND)運算符分隔他們。
expression [-a] expression
Conjunction of primaries; the AND operator is implied by the juxtaposition of
two primaries or made explicit by the optional -a operator.
The second expression shall not be evaluated if the first expression is false.
結論
所以下面的posix
標準必須執行expressions
左至右任何執行;和
find . -type d -mount -ctime +5 -prune -exec 'rm {}' \;
相當於
find . -type d -a -mount -a -ctime +5 -a -prune -a -exec 'rm {}' \;
這意味着,如果所有的先行operands
屬實-exec
纔會執行。
其他
另外值得一提的是,雖然-exec
可能不叫你的發現實現一個明確的action
,它應該仍然可以以類似的方式處理(即更換-print
動作)
If no expression is present, -print shall be used as the expression.
Otherwise, if the given expression does not contain any of the primaries -exec,
-ok, or -print, the given expression shall be effectively replaced by:
(given_expression) -print
編輯
技術上是真實的,並非每一個IMPL ementation必須是posix投訴。從solaris find man page
expression [-a] expression
Concatenation of primaries (the and operation is implied by the juxtaposition of two primaries).
無關的答案He're幾乎相同的報價,但我不認爲'RM {}'應該被引用。如果你的shell會以某種方式首先解釋它,或者在大多數shell中不加引號,你可以引用'{}'。 – BroSlow 2014-10-03 15:01:43