[英]allow user to complete parallel / xargs command (function) after selecting files into array; quoting nuls correctly in printf script
[英]How to use xargs on an array of files
我想将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"
但是它仅对数组abc的第一个元素起作用。
由于您使用的非标准-maxdepth
,我假定非标准find
一个处理-print0
谓词,以便安全地创建数组 :
abc=()
while IFS= read -r -d '' f; do
abc+=( "$f" )
done < <(find . -maxdepth 3 -type f -name "abc.txt" -print0)
那里有一个数组 abc
。
现在,您可以安全地将数组abc
作为参数传递给grep
:
grep -l "somestring1" "${abc[@]}"
grep -l "somestring2" "${abc[@]}"
grep -l "somestring3" "${abc[@]}"
注意1.在您的代码中, abc
不是数组。
注2:我不知道为什么您说您的代码不起作用……它应该起作用(前提是您的文件名没有空格和引号); 也许您没有显示完整的代码?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.