Perl:试图将ls -l放入数组中会变得混乱

时间:2014-03-11 05:46:15

标签: perl unix

我从这个简单的代码开始,它通过/ home /并确定对象是文件还是目录

#!/usr/bin/perl


# My Starting directory.  I could also read this from the command line
$start = "/home/";
$count = 0; # How many non-file objects found.  Line un-necessary

# Initialize the list
push (@dirs, $start);

# Iterate through the list (really a queue) 
# We could also do this with a shift, but this works
foreach $curr (@dirs)
    {
    # Get the directory listing for the current directory
    # Note that -F appends a character for the type of object it is
    # on the end (/) for directory.


    my @lines = split /\n/, `ls -F $curr`;

    # Iterate through the things we got from the ls
    foreach $line (@lines)
    {
        # The chomp is not necessary because the split strips out
        # the seperator characters.
        #chomp $line;

        # If it ends in a / it's a directory. add it to the end of
        # the list
        if ($line =~ m#/$#)
        {
            print "Directory of -> " . $curr.$line . "\n";
            push(@dirs, ($curr.$line));
        }
        else
        {
            print "File of ==> " . $curr.$line . "\n";
            $count++;
        }
    }
}

# Silly print statement
print "I found " . $count . " non-directory objects.\n";

我尝试修改它以使用 ls -l <​​/ strong>,因为程序的其余部分取决于它的信息,但这就是事情变得奇怪的地方。

我修改了什么:

my @lines = `ls -l $curr`;

if ($line =~ m#-.........#)

elsif ($line =~ m#d.........#)

我不是故意保持正则表达式,一旦我让它真正正确地进行,我将以正确的方式去做。

我在foreach

之后添加了一个愚蠢的调试
print("$line \n");

我应该得到的是这样的:

File of ==> /home/paul/perl/spider*
Directory of -> /home/paul/perl/test/
I found 9 non-directory objects.

相反,我明白了:

total 8

drwxr-xr-x 28 paul paul 4096 2014-03-11 01:31 paul

Directory of -> /home/drwxr-xr-x 28 paul paul 4096 2014-03-11 01:31 paul

drwxr-xr-x  2 test test 4096 2014-03-10 02:12 test

Directory of -> /home/drwxr-xr-x  2 test test 4096 2014-03-10 02:12 test

ls: cannot access /home/drwxr-xr-x: No such file or directory
ls: cannot access 28: No such file or directory
ls: cannot access paul: No such file or directory
ls: cannot access paul: No such file or directory
ls: cannot access 4096: No such file or directory
ls: cannot access 2014-03-11: No such file or directory
ls: cannot access 01:31: No such file or directory
ls: cannot access paul: No such file or directory
ls: cannot access /home/drwxr-xr-x: No such file or directory
ls: cannot access 2: No such file or directory
ls: cannot access 4096: No such file or directory
ls: cannot access 2014-03-10: No such file or directory
ls: cannot access 02:12: No such file or directory
test:

total 0



test:

total 0



test:

total 0

I found 0 non-directory objects.

我已经结束了,并且不明白为什么我尝试过的其他事情也都失败了。提前谢谢!

1 个答案:

答案 0 :(得分:6)

如果需要目录递归,File::Find可能比滚动自己的目录遍历逻辑更好,更容易。但由于问题主要是解析ls,我将更详细地介绍一下。

Parsing ls output有很多并发症。正确的方法是自己处理目录。

opendir(my $d, "/path/to/dir") or die "$!";
while ($item = readdir($d)) {
    if (-d $item) {
        print "dir $item\n";
    } else {
        print "notdir $item\n";
    }
}
closedir ($d);

除了-d之外还有许多其他file test operators。如果您想要更详细地控制每个项目,请查看stat