如何将find命令的输出存储在数组中? + bash

时间:2016-01-25 01:35:40

标签: arrays bash find

我有以下命令,输出如下:

$ find -name '*.jpg'
./public_html/github/screencasts-gh-pages/reactiveDataVis/presentation/images/telescope.jpg
./public_html/github/screencasts-gh-pages/introToBackbone/presentation/images/telescope.jpg
./public_html/github/StarCraft-master/img/Maps/(6)Thin Ice.jpg
./public_html/github/StarCraft-master/img/Maps/Snapshot.jpg
./public_html/github/StarCraft-master/img/Maps/Map_Grass.jpg
./public_html/github/StarCraft-master/img/Maps/(8)TheHunters.jpg
./public_html/github/StarCraft-master/img/Maps/(2)Volcanis.jpg
./public_html/github/StarCraft-master/img/Maps/(3)Trench wars.jpg
./public_html/github/StarCraft-master/img/Maps/(8)BigGameHunters.jpg
./public_html/github/StarCraft-master/img/Maps/(8)Turbo.jpg
./public_html/github/StarCraft-master/img/Maps/(4)Blood Bath.jpg
./public_html/github/StarCraft-master/img/Maps/(2)Switchback.jpg
./public_html/github/StarCraft-master/img/Maps/Original/(6)Thin Ice.jpg
./public_html/github/StarCraft-master/img/Maps/Original/Map_Grass.jpg
./public_html/github/StarCraft-master/img/Maps/Original/(8)TheHunters.jpg
./public_html/github/StarCraft-master/img/Maps/Original/(2)Volcanis.jpg
./public_html/github/StarCraft-master/img/Maps/Original/(3)Trench wars.jpg
./public_html/github/StarCraft-master/img/Maps/Original/(8)BigGameHunters.jpg
./public_html/github/StarCraft-master/img/Maps/Original/(8)Turbo.jpg
./public_html/github/StarCraft-master/img/Maps/Original/(4)Blood Bath.jpg
./public_html/github/StarCraft-master/img/Maps/Original/(2)Switchback.jpg
./public_html/github/StarCraft-master/img/Maps/Original/(4)Orbital Relay.jpg
./public_html/github/StarCraft-master/img/Maps/(4)Orbital Relay.jpg
./public_html/github/StarCraft-master/img/Bg/GameLose.jpg
./public_html/github/StarCraft-master/img/Bg/GameWin.jpg
./public_html/github/StarCraft-master/img/Bg/GameStart.jpg
./public_html/github/StarCraft-master/img/Bg/GamePlay.jpg
./public_html/github/StarCraft-master/img/Demo/Demo.jpg
./public_html/github/flot/examples/image/hs-2004-27-a-large-web.jpg
./public_html/github/minicourse-ajax-project/other/GameLose.jpg

如何将此输出存储在数组中?我希望它处理带空格的文件名

我试过这个arrayname=($(find -name '*.jpg')),但这只是存储了第一个元素。 @我正在做以下似乎只是第一个元素?

$ arrayname=($(find -name '*.jpg'))
$ echo "$arrayname"
./public_html/github/screencasts-gh-pages/reactiveDataVis/presentation/images/telescope.jpg
$ 

我尝试了here但是这只是存储第一个元素

其他类似的Qs How do I capture the output from the ls or find command to store all file names in an array?
How do i store the output of a bash command in a variable?

2 个答案:

答案 0 :(得分:3)

如果您确定知道您的文件名不包含换行符,那么

mapfile -t arrayname < <(find ...)

如果您希望能够处理任何文件

arrayname=()
while IFS= read -d '' -r filename; do
    arrayname+=("$filename")
done < <(find ... -print0)

echo "$arrayname"只会显示数组的第一个元素。它相当于echo "${arrayname[0]}"。转储数组:

printf "%s\n" "${arrayname[@]}"
# ............^^^^^^^^^^^^^^^^^ must use exactly this form, with the quotes.

arrayname=($(find ...))仍然是错误的。它将文件./file with spaces.txt存储为数组中的3个独立元素。

答案 1 :(得分:1)

如果你有一个足够新版的bash,只需使用** glob就可以省去很多麻烦。

shopt -s globstar
files=(**/*.jpg)

第一行启用该功能。启用后,glob模式中的**将匹配路径中任意数量(包括0)的目录。

在数组定义中使用glob可确保正确处理空格。

要以可用于定义数组的形式查看数组,请使用-p内置的declare(打印)选项:

declare -p files