bash 将文件名读入数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10981439/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:13:31  来源:igfitidea点击:

Reading filenames into an array

bashunixcygwin

提问by dublintech

I want to get a list of files and then read the results into an array where each array element corresponds to a file name. Is this possible?

我想获取一个文件列表,然后将结果读入一个数组,其中每个数组元素对应一个文件名。这可能吗?

回答by Paused until further notice.

Don't use ls, it's not intendedfor this purpose. Use globbing.

不要使用ls,它不是用于此目的。使用通配符。

shopt -s nullglob
array=(*)
array2=(file*)
array3=(dir/*)

The nullgloboption causes the array to be empty if there are no matches.

nullglob如果没有匹配项,该选项会导致数组为空。

回答by anubhava

Following will create an array arr with ls output in current directory:

以下将在当前目录中创建一个带有 ls 输出的数组 arr:

arr=( $(ls) )

Though using output of lsis not safe at all.

虽然使用的输出ls根本不安全。

Much better and safer than lsyou can use echo *:

ls您可以使用的更好和更安全echo *

arr=( * )

echo ${#arr[@]} # will echo number of elements in array

echo "${arr[@]}" # will dump all elements of the array

回答by xizdaqrian

Actually, lsisn't the way to go. Try this:

其实,ls也不是办法。尝试这个:

declare -a FILELIST
for f in *; do 
    #FILELIST[length_of_FILELIST + 1]=filename
    FILELIST[${#FILELIST[@]}+1]=$(echo "$f");
done

To get a filename from the array use:

要从数组中获取文件名,请使用:

echo ${FILELIST[x]}

To get n filenames from the array starting from x use:

要从从 x 开始的数组中获取 n 个文件名,请使用:

echo ${FILELIST[@]:x:n}

For a great tutorial on bash arrays, see: http://www.thegeekstuff.com/2010/06/bash-array-tutorial/

有关 bash 数组的精彩教程,请参阅:http: //www.thegeekstuff.com/2010/06/bash-array-tutorial/

回答by Josh Habdas

In bash you can create an array of filenames with pathname expansion (globbing)like so:

在 bash 中,您可以创建一个带有路径名扩展(通配)的文件数组,如下所示:

#!/bin/bash
SOURCE_DIR=path/to/source
files=(
   "$SOURCE_DIR"/*.tar.gz
   "$SOURCE_DIR"/*.tgz
   "$SOURCE_DIR"/**/*
)

The above will create an array called filesand add to it N array elements, where each element in the array corresponds to an item in SOURCE_DIRending in .tar.gzor .tgz, or any item in a subdirectory thereof with subdirectory recursion possible as Dennis points outin the comments.

上面将创建一个名为的数组files并向其添加 N 个数组元素,其中数组中的每个元素对应于SOURCE_DIR.tar.gzor结尾的项目.tgz,或者其子目录中的任何项目,正如丹尼斯在评论中指出的那样,子目录递归是可能的。

You can then use printfto see the contents of the array including paths:

然后,您可以使用printf来查看数组的内容,包括路径:

printf '%s\n' "${files[@]}" # i.e. path/to/source/filename.tar.gz

Or using parameter substitutionto exclude the pathnames:

或者使用参数替换来排除路径名:

printf '%s\n' "${files[@]##*/}" # i.e. filename.tgz

回答by KSU_Physics

Try this,

尝试这个,

path="" # could set to any absolute path
declare -a array=( "${path}"/* )

I'm assuming you'll pull out the unwanted stuff from the list later.

我假设你稍后会从列表中取出不需要的东西。