[英]How to use xargs on an array of files
I want to store the find command output in a variable and use it many times as find command consumes time. 我想将find命令的输出存储在一个变量中,并多次使用它,因为find命令会消耗时间。
abc=`find . -maxdepth 3 -type f -name "abc.txt"`
echo "${abc[*]}" | xargs grep -l "somestring1"
echo "${abc[*]}" | xargs grep -l "somestring2"
echo "${abc[*]}" | xargs grep -l "somestring3"
But it only greps on the first element of array abc. 但是它仅对数组abc的第一个元素起作用。
Since you're using nonstandard -maxdepth
, I'm assuming non-standard find
that handles the -print0
predicate, so as to safely build an array : 由于您使用的非标准
-maxdepth
,我假定非标准find
一个处理-print0
谓词,以便安全地创建数组 :
abc=()
while IFS= read -r -d '' f; do
abc+=( "$f" )
done < <(find . -maxdepth 3 -type f -name "abc.txt" -print0)
There you have an array abc
. 那里有一个数组
abc
。
Now, you can safely pass the array abc
as arguments to grep
: 现在,您可以安全地将数组
abc
作为参数传递给grep
:
grep -l "somestring1" "${abc[@]}"
grep -l "somestring2" "${abc[@]}"
grep -l "somestring3" "${abc[@]}"
Note 1. In your code, abc
is not an array. 注意1.在您的代码中,
abc
不是数组。
Note 2. I have no idea why you say your code doesn't work… it should work (provided you have nice filenames without spaces and quotes); 注2:我不知道为什么您说您的代码不起作用……它应该起作用(前提是您的文件名没有空格和引号); maybe you're not showing the full code?
也许您没有显示完整的代码?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.