将文件名读入数组 [英] Reading filenames into an array

查看:27
本文介绍了将文件名读入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

不要使用 ls,它是 不是为了这个目的.使用通配符.

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

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

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

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

这篇关于将文件名读入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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