读取的文件名到一个数组 [英] Reading filenames into an array

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

问题描述

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

I want to run a ls to get a list of files and then read the result into an array where each 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 matched.

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

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