1

ls -l

-rw-r--r-- 1 angus angus    0 2013-08-16 01:33 copy.pl
-rw-r--r-- 1 angus angus 1931 2013-08-16 08:27 copy.txt
-rw-r--r-- 1 angus angus  492 2013-08-16 03:15 ex.txt
-rw-r--r-- 1 angus angus   25 2013-08-16 09:07 hello.txt
-rw-r--r-- 1 angus angus   98 2013-08-16 09:05 hi.txt

我只需要读取、写入、访问数据以及文件名。

#! /usr/bin/perl -w
@list = `ls -l`;
$index = 0;
#print "@list\n";
for(@list){
 ($access) = split(/[\s+]/,$_);
 print "$access\n";
 ($data) = split(/pl+/,$_);
 print "$data";
 @array1 = ($data,$access);
}
print "@array1\n"

我编写了这段代码来提取读、写、访问权限详细信息以及与之对应的文件名。我无法提取最后一列的文件名。

4

2 回答 2

7

查看 perl stat http://perldoc.perl.org/functions/stat.html 比调用外部ls命令更加健壮和高效,

use File::stat;
$sb = stat($filename);
printf "File is %s, size is %s, perm %04o, mtime %s\n",
       $filename, $sb->size, $sb->mode & 07777,
       scalar localtime $sb->mtime;
于 2013-08-17T07:12:51.973 回答
2

我认为您的脚本第 8 行有错误。您正在尝试使用字符串“pl”作为分隔符来拆分行,该分隔符只会匹配您输入的第一行,并且不会给您我认为您想要的内容。

我相信你应该在空白处分割整行并只分配你想要的列(在这种情况下为数字 1 和 8)。

为此改变你的循环:

for my $filename (@list){
    chomp($filename);
    my ($access, $data) = (split(/\s+/, $filename))[0, 7]; #use a slice to get only the columns you want.
    print "$access $data\n";
}

注意: mpapec 建议使用 Stat 会更好。我只是想让您知道为什么您的代码不起作用。

于 2013-08-17T18:56:52.067 回答