【问题标题】:How do I store the output from a find command in an array? + bash如何将 find 命令的输出存储在数组中? + 重击
【发布时间】:2016-05-01 05:53:27
【问题描述】:

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

$ 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,但这只是存储第一个元素

其他类似的Q
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?

【问题讨论】:

  • 为我工作。为什么你认为你得到的只是第一个元素?使用什么代码来验证它? (再一次,它有一个问题,它会分割包含空格的行。)
  • 我在那个数组上做一个回显,它只是显示第一个元素,你能纠正我的方法吗,看看我编辑的 Q
  • 如果您正在执行echo $arrayname,则显示第一个元素。如果你在做echo "${arrayname[@]}",它会显示整个数组。
  • 我可以打印成这种格式['a','b','c'...]吗?
  • 工作,是的。自动,没有。如果您想要方便,请使用 Ruby 或其他不错的脚本语言。

标签: arrays bash find


【解决方案1】:

如果你确定你的文件名不会包含换行符,那么

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 个独立元素。

【讨论】:

  • 试过$ arrayname=() $ while IFS= read -d '' -r filename; do &gt; arrayname+=("$filename") &gt; done &lt; &lt;(find '*.jpg' -print0) find: *.jpg': No such file or directory $` 得到这个错误find: *.jpg': No such file or directory`
  • @glennjackman 这是一个很好的答案。我也可以添加这个 while read -rd '';do arr[i++]=$REPLY;done &lt; &lt;(find . -name '*.jpg' -print0) ,在 Bash 中可执行。
【解决方案2】:

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

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

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

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

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

declare -p files

【讨论】:

  • 非常好。我刚刚回答了这个问题,但这解决了问题。
  • 每当我启用 globstar 时,我也会使用 nullglob
猜你喜欢
  • 2012-01-03
  • 2017-09-20
  • 2011-06-08
  • 2018-08-30
  • 1970-01-01
  • 2015-03-10
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多