我怎么能存储find命令的结果在Bash中的数组 [英] How can I store find command result as arrays in Bash

查看:769
本文介绍了我怎么能存储find命令的结果在Bash中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到保存作为结果的数组。
这里是我的code:

I am trying to save find result as arrays. Here is my code:

#!/bin/bash

echo "input : "
read input

echo "searching file with this pattern '${input}' under present directory"
array=`find . -name ${input}`

len=${#array[*]}
echo "found : ${len}"

i=0

while [ $i -lt $len ]
do
echo ${array[$i]}
let i++
done

我得到当前目录下2 .txt文件。
所以,我希望2 $ {LEN} 的结果。但是,它打印1。
为什么它需要找到为一个元素的所有结果的原因。
我该如何解决这个问题?谢谢你。

I get 2 .txt files under current directory. So I expect '2' as result of ${len}. However, it prints 1. The reason why is it takes all result of find as one elements. How can I fix this? Thanks.

P.S
我发现了几个解决方案的StackOverflow有关类似的问题。然而,这是有点不同的,所以我可以申请我的。我需要存储结果循环之前变量。再次感谢。

P.S I found several solutions in StackOverFlow about similar problem. However, it is little bit different so I can apply to mine. I need to store result to variable before loop. Thanks again.

推荐答案

下面是获得的输出找到一个解决办法庆典数组:

array=()
while IFS=  read -r -d $'\0'; do
    array+=("$REPLY")
done < <(find . -name ${input} -print0)

这是棘手的,因为,在一般情况下,文件名可以有空格,新行及其他脚本的敌对角色。只有这样,才能使用找到和具有彼此是使用 -print0 打印出安全分隔的文件名文件名以空字符分隔。这不会是多大的不便,如果bash中的 readarray / 映射文件函数支持空分隔的字符串,但他们不这样做。击的不和,导致我们上面的循环。

This is tricky because, in general, file names can have spaces, new lines, and other script-hostile characters. The only way to use find and have the file names safely separated from each other is to use -print0 which prints the file names separated with a null character. This would not be much of an inconvenience if bash's readarray/mapfile functions supported null-separated strings but they don't. Bash's read does and that leads us to the loop above.


  1. 第一行创建一个空数组: =阵列()

语句执行每一次,一个空分隔的文件名是从标准输入读取。在 -r 选项告诉独自离开反斜杠字符。在 -d $'\\ 0'告诉,输入将是空分隔。

Every time that the read statement is executed, a null-separated file name is read from standard input. The -r option tells read to leave backslash characters alone. The -d $'\0' tells read that the input will be null-separated.

阵列+ =($ REPLY)语句追加新的文件名数组阵列

The array+=("$REPLY") statement appends the new file name to the array array.

最后一行结合重定向和命令替换为的输出找到,而循环。

The final line combines redirection and command substitution to provide the output of find to the standard input of the while loop.

如果我们不使用过程中的替代,循环可以写成:

Why use process substitution?

If we didn't use process substitution, the loop could be written as:

array=()
find . -name ${input} -print0 >tmpfile
while IFS=  read -r -d $'\0'; do
    array+=("$REPLY")
done <tmpfile
rm -f tmpfile

在输出上述找到存储在一个临时文件,该文件作为标准输入while循环。过程替代的思路是使这样的临时文件不必要的。所以,与其具有,而循环从 TMPFILE 获得它的标准输入,我们可以从<$得到它的标准输入C $ C>≤(。找到-name $ {}输入-print0)

In the above the output of find is stored in a temporary file and that file is used as standard input to the while loop. The idea of process substitution is to make such temporary files unnecessary. So, instead of having the while loop get its stdin from tmpfile, we can have it get its stdin from <(find . -name ${input} -print0).

进程替换是广泛有用的。在许多地方,一个命令想要的阅读的从一个文件,你可以指定函数替换&LT;的(...),而不是文件名。有一个类似的形式,&GT;(...),可以在地方,命令想要一个文件名使用的的到该文件。

Process substitution is widely useful. In many places where a command wants to read from a file, you can specify process substitution, <(...), instead of a file name. There is an analogous form, >(...), that can be used in place of a file name where the command wants to write to the file.

数组一样,进程替换是bash和其他先进的炮弹的一个特点。这不是POSIX标准的一部分。

Like arrays, process substitution is a feature of bash and other advanced shells. It is not part of the POSIX standard.

下面的命令创建一个shell变量,而不是一个shell数组:

The following command creates a shell variable, not a shell array:

array=`find . -name ${input}`

如果你想创建一个数组,你需要把寻找周围的输出括号。所以,天真,我们可以:

If you wanted to create an array, you would need to put parens around the output of find. So, naively, one could:

array=(`find . -name ${input}`)  # don't do this

问题是,外壳上的结果进行分词找到这样的数组的元素是不能保证是你想要的。

The problem is that the shell performs word splitting on the results of find so that the elements of the array are not guaranteed to be what you want.

这篇关于我怎么能存储find命令的结果在Bash中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆