Xargs
如何使用 xargs -I 減少執行次數
該
find
命令有兩種方式來執行某種xargs
風格的東西。find foo -exec bar '{}' baz ';' find foo -exec bar '{}' baz '+'
它們之間的區別在於,第一個將每個找到的文件執行一次,第二個將對文件進行分組以減少執行次數。
當我看它時,
xargs
它有-I
開關,例如:xargs -I{} foo bar {} baz
這將每個 arg 執行一次。
問題:
xargs
有 沒有辦法減少執行次數find
?我想做這樣的事情:
xargs -I{} echo start {} end << EOF hello world EOF
並得到結果:
start hello world end
不是:
start hello end start world end
我認為這樣做的唯一方法是為您的命令製作一個簡單的包裝腳本:
#!/bin/sh echo start "$@" end
那麼你可以使用
xargs echo-wrapper
當然,您可以內聯執行此操作:
xargs sh -c 'echo start "$@" end' sh <<EOF hello world EOF
請注意最後的額外內容——這是語法
sh
的一部分,它在 shell 內部指定。sh -c``$0