相关疑难解决方法(0)

如何将目录文件列表存储到数组中?

我正在尝试将文件列表存储到一个数组中,然后再次遍历该数组.下面是我ls -ls从控制台运行命令时得到的内容.

total 40
36 -rwxrwxr-x 1 amit amit 36720 2012-03-31 12:19 1.txt
4 -rwxrwxr-x 1 amit amit  1318 2012-03-31 14:49 2.txt
Run Code Online (Sandbox Code Playgroud)

我写的以下bash脚本将上述数据存储到bash数组中.

i=0
ls -ls | while read line
do
    array[ $i ]="$line"        
    (( i++ ))
done
Run Code Online (Sandbox Code Playgroud)

但是当我echo $array,我什么都没得到!

仅供参考,我这样运行脚本: ./bashscript.sh

bash shell

40
推荐指数
4
解决办法
10万
查看次数

标签 统计

bash ×1

shell ×1