[英]How do I store the output from a find command in an array? + bash
I have the following find command with the following output: 我有以下输出以下命令的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
How do I store this output in an array? 如何将此输出存储在数组中? I want it to handle filenames with spaces
我希望它处理带空格的文件名
I have tried this arrayname=($(find -name '*.jpg'))
but this just stores the first element. 我已经尝试过这个
arrayname=($(find -name '*.jpg'))
但这只是存储第一个元素。 @ I am doing the following which seems to be just the first element? @我正在执行以下操作,但这似乎只是第一个元素?
$ arrayname=($(find -name '*.jpg'))
$ echo "$arrayname"
./public_html/github/screencasts-gh-pages/reactiveDataVis/presentation/images/telescope.jpg
$
I have tried here but again this just stores the 1st element 我在这里尝试过,但再次这只是存储第一个元素
Other similar Qs 其他类似的问题
How do I capture the output from the ls or find command to store all file names in an array? 如何捕获ls或find命令的输出以将所有文件名存储在数组中?
How do i store the output of a bash command in a variable? 如何将bash命令的输出存储在变量中?
If you know with certainty that your filenames will not contain newlines, then 如果您确定您的文件名将不包含换行符,那么
mapfile -t arrayname < <(find ...)
If you want to be able to handle any file 如果您希望能够处理任何文件
arrayname=()
while IFS= read -d '' -r filename; do
arrayname+=("$filename")
done < <(find ... -print0)
echo "$arrayname"
will only show the first element of the array. echo "$arrayname"
将仅显示数组的第一个元素。 It is equivalent to echo "${arrayname[0]}"
. 等效于
echo "${arrayname[0]}"
。 To dump an array: 转储数组:
printf "%s\n" "${arrayname[@]}"
# ............^^^^^^^^^^^^^^^^^ must use exactly this form, with the quotes.
arrayname=($(find ...))
is still wrong. arrayname=($(find ...))
仍然是错误的。 It will store the file ./file with spaces.txt
as 3 separate elements in the array. 它将文件
./file with spaces.txt
存储为数组中的3个独立元素。
If you have a sufficiently recent version of bash, you can save yourself a lot of trouble by just using a **
glob. 如果您使用的bash版本足够新,则只需使用
**
glob即可为您省去很多麻烦。
shopt -s globstar
files=(**/*.jpg)
The first line enables the feature. 第一行启用该功能。 Once enabled,
**
in a glob pattern will match any number (including 0) of directories in the path. 启用后,全局模式中的
**
将匹配路径中任意数量(包括0)的目录。
Using the glob in the array definition makes sure that whitespace is handled correctly. 在数组定义中使用glob可以确保正确处理空白。
To view an array in a form which could be used to define the array, use the -p
(print) option to the declare
builtin: 要以可用于定义数组的形式查看数组,请对内置的
declare
使用-p
(打印)选项:
declare -p files
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.