bash 将 find 命令的输出存储在一个数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8213328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Store the output of find command in an array
提问by Gábor Varga
How do I put the result of find $1
into an array?
如何将 的结果find $1
放入数组?
In for loop:
在 for 循环中:
for /f "delims=/" %%G in ('find ') do %%G | cut -d\/ -f6-
采纳答案by gustavotkg
To loop through a find, you can simply use find:
要循环查找,您可以简单地使用 find:
for file in "`find ""`"; do
echo "$file" | cut -d/ -f6-
done
It was what I got from your question.
这是我从你的问题中得到的。
回答by sorpigal
I want to cry.
我想哭。
In bash:
在 bash 中:
file_list=()
while IFS= read -d $'for file in "${file_list[@]}" ; do
echo "$file" | cut -d/ -f6-
done
' -r file ; do
file_list=("${file_list[@]}" "$file")
done < <(find "" -print0)
echo "${file_list[@]}"
file_list
is now an array containing the results of find "$1
file_list
现在是一个包含结果的数组 find "$1
What's special about "field 6"? It's not clear what you were attempting to do with your cut
command.
“6号场”有什么特别之处?目前尚不清楚您试图用您的cut
命令做什么。
Do you want to cut each file after the 6th directory?
是否要剪切第 6 个目录之后的每个文件?
for file in "${file_list[@]}" ; do
echo "${file##*/}"
done
But why "field 6"? Can I presume that you actually want to return just the last element of the path?
但为什么是“字段 6”?我可以假设您实际上只想返回路径的最后一个元素吗?
echo "${file_list[@]##*/}"
Or even
甚至
for file in "${file_list[@]##*/}" ; do
echo "$file"
done
Which will give you the last path element for each path in the array. You could even do something with the result
这将为您提供数组中每个路径的最后一个路径元素。你甚至可以对结果做些什么
find "" -print0
Explanation of the bash program elements:
bash程序元素说明:
(One should probably use the builtin readarray
instead)
(人们可能应该改用内置函数readarray
)
<(find "" -print0)
Find stuff and 'print the full file name on the standard output, followed by a null character'. This is important as we will split that output by the null character later.
查找内容并“在标准输出上打印完整的文件名,后跟一个空字符”。这很重要,因为我们稍后将通过空字符分割该输出。
while ...
done < <(find "" -print0)
"Process Substitution" : The output of the find
subprocess is read in via a FIFO (i.e. the output of the find
subprocess behaves like a file here)
“进程替换”:find
子进程的输出通过 FIFO 读入(即子进程的输出在find
这里表现得像一个文件)
IFS= read -d $'read
' -r file
The output of the find
subprocess is read by the while
command via <
命令通过以下find
方式读取子进程的输出while
<
-d $'-r
'
This is the while condition:
这是while条件:
file
Read one line of input (from the find command). Returnvalue of read
is 0 unless EOF is encountered, at which point while exits.
读取一行输入(来自 find 命令)。read
除非遇到 EOF,否则返回值为 0,此时 while 退出。
IFS=
...taking as delimiter the null character (see QUOTING in bash manpage). Which is done because we used the null character using -print0
earlier.
...将空字符作为分隔符(请参阅 bash 联机帮助页中的 QUOTING)。这样做是因为我们使用了-print0
之前使用的空字符。
file_list=("${file_list[@]}" "$file")
backslash is not considered an escape character as it may be part of the filename
反斜杠不被视为转义字符,因为它可能是文件名的一部分
arrayname=( $(find ) )
Result (first word actually, which is unique here) is put into variable file
结果(实际上是第一个词,在这里是唯一的)被放入变量中 file
for element in $(seq 0 $((${#arrayname[@]} - 1)))
do
echo "${arrayname[$element]}"
done
The command is run with IFS
, the special variable which contains the characters on which read
splits input into words unset. Because we don't want to split.
该命令与 一起运行IFS
,该特殊变量包含read
将输入拆分为未设置单词的字符。因为我们不想分开。
And inside the loop:
在循环内:
declare -a BASH_ARRAY_VARIABLE=$(find <path> <other options> -print0 | sed -e 's/\x0$//' | awk -F'for FIND_PATH in "${BASH_ARRAY_VARIABLE[@]}"; do echo "$FIND_PATH"; done
' 'BEGIN { printf "("; } { for (i = 1; i <= NF; i++) { printf "%c"gensub(/"/, "\\\"", "g", $i)"%c ", 34, 34; } } END { printf ")"; }')
Inside the loop, the file_list array is just grown by $file
, suitably quoted.
在循环内部,file_list 数组只是增加了$file
,适当引用。
回答by Cougar
declare -a names=$(echo "("; find <path> <other options> -printf '"%p" '; echo ")")
for nm in "${names[@]}"
do
echo "$nm"
done
I don't understand your loop question? If you look how to work with that array then in bash you can loop through all array elements like this:
我不明白你的循环问题?如果您查看如何使用该数组,那么在 bash 中您可以像这样循环遍历所有数组元素:
##代码##回答by user2618594
This is probably not 100% foolproof, but it will probably work 99% of the time (I used the GNU utilities; the BSD utilities won't work without modifications; also, this was done using an ext4 filesystem):
这可能不是 100% 万无一失,但它可能会在 99% 的时间内工作(我使用了 GNU 实用程序;如果不修改 BSD 实用程序将无法运行;此外,这是使用 ext4 文件系统完成的):
##代码##Then you would iterate over it like so:
然后你会像这样迭代它:
##代码##Make sure to enclose $FIND_PATH inside double-quotes when working with the path.
使用路径时,请确保将 $FIND_PATH 括在双引号内。
回答by R Risack
Here's a simpler pipeless version, based on the version of user2618594
这是一个更简单的无管道版本,基于user2618594的版本
##代码##