2014-10-03 156 views
0

我一直在處理man手冊,不知道find如何執行測試順序。find中的評估順序

請注意Solaris的find命令沒有單獨的「測試」和「動作」,而只是測試表達式的片段。

在這種情況下,對於像

find . -type d -mount -ctime +5 -prune -exec 'rm {}' \; 

命令行能夠保證所有的 「式d」 和 「-ctime 5」 的前評估 「-exec 'RM {}' \;」因此,只有正確的文件被刪除?

+0

無關的答案He're幾乎相同的報價,但我不認爲'RM {}'應該被引用。如果你的shell會以某種方式首先解釋它,或者在大多數shell中不加引號,你可以引用'{}'。 – BroSlow 2014-10-03 15:01:43

回答

0

說明

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). 
+1

這並不是「按照要求保證」,而是近在咫尺。非常感謝。 – Envite 2014-10-06 05:16:56

+0

@Envite不是每個實現都必須符合'posix'。從'solaris find'手冊頁添加了引用。 – BroSlow 2014-10-06 06:48:55

+1

是的,正是我所看到的那個,沒有對「如果第一個表達式是錯誤的,則不應評估第二個表達式」。這引出了我的問題。 – Envite 2014-10-06 11:13:37