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

时间:2021-07-08 00:08:42

I'm trying to store the files listing into an array and then loop through the array again. Below is what I get when I run ls -ls command from the console.

我尝试将文件列表存储到一个数组中,然后再循环遍历数组。下面是我从控制台运行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

The following bash script I've written to store the above data into a bash array.

下面我编写的bash脚本将上述数据存储到bash数组中。

i=0
ls -ls | while read line
do
    array[ $i ]="$line"        
    (( i++ ))
done

But when I echo $array, I get nothing!

但是当我回显$array时,我什么也得不到!

FYI, I run the script this way: ./bashscript.sh

我用这种方式运行脚本:./bashscript.sh

5 个解决方案

#1


29  

Try with:

试一试:

#! /bin/bash

i=0
while read line
do
    array[ $i ]="$line"        
    (( i++ ))
done < <(ls -ls)

echo ${array[1]}

In your version, the while runs in a subshell, the environment variables you modify in the loop are not visible outside it.

在您的版本中,当在子shell中运行时,您在循环中修改的环境变量在它之外是不可见的。

(Do keep in mind that parsing the output of ls is generally not a good idea at all.)

(请记住,解析ls的输出通常根本不是一个好主意。)

#2


73  

I'd use

我使用

files=(*)

And then if you need data about the file, such as size, use the stat command on each file.

然后,如果您需要关于文件的数据,例如大小,请在每个文件上使用stat命令。

#3


4  

This might work for you:

这可能对你有用:

OIFS=$IFS; IFS=$'\n'; array=($(ls -ls)); IFS=$OIFS; echo "${array[1]}"

#4


4  

Here's a variant that lets you use a regex pattern for initial filtering, change the regex to be get the filtering you desire.

这里有一个变体,允许您使用regex模式进行初始过滤,将regex更改为获取所需的过滤。

files=($(find -E . -type f -regex "^.*$"))
for item in ${files[*]}
do
  printf "   %s\n" $item
done

#5


0  

Running any shell command inside $(...) will help to store the output in a variable. So using that we can convert the files to array with IFS.

在$(…)中运行任何shell命令将有助于将输出存储在一个变量中。这样我们就可以用IFS把文件转换成数组。

IFS=' ' read -r -a array <<< $(ls /path/to/dir)

#1


29  

Try with:

试一试:

#! /bin/bash

i=0
while read line
do
    array[ $i ]="$line"        
    (( i++ ))
done < <(ls -ls)

echo ${array[1]}

In your version, the while runs in a subshell, the environment variables you modify in the loop are not visible outside it.

在您的版本中,当在子shell中运行时,您在循环中修改的环境变量在它之外是不可见的。

(Do keep in mind that parsing the output of ls is generally not a good idea at all.)

(请记住,解析ls的输出通常根本不是一个好主意。)

#2


73  

I'd use

我使用

files=(*)

And then if you need data about the file, such as size, use the stat command on each file.

然后,如果您需要关于文件的数据,例如大小,请在每个文件上使用stat命令。

#3


4  

This might work for you:

这可能对你有用:

OIFS=$IFS; IFS=$'\n'; array=($(ls -ls)); IFS=$OIFS; echo "${array[1]}"

#4


4  

Here's a variant that lets you use a regex pattern for initial filtering, change the regex to be get the filtering you desire.

这里有一个变体,允许您使用regex模式进行初始过滤,将regex更改为获取所需的过滤。

files=($(find -E . -type f -regex "^.*$"))
for item in ${files[*]}
do
  printf "   %s\n" $item
done

#5


0  

Running any shell command inside $(...) will help to store the output in a variable. So using that we can convert the files to array with IFS.

在$(…)中运行任何shell命令将有助于将输出存储在一个变量中。这样我们就可以用IFS把文件转换成数组。

IFS=' ' read -r -a array <<< $(ls /path/to/dir)