#!/usr/bin/env perl
# filename: findfile.pl

use strict;
use File::Find;

# 1. variables
my @file_dirs = qw{/etc};

# 2. main
my @contents = find(\&wanted, @file_dirs);
open MAIL, "> |mail -s xxx [email protected]" or die
    "Open file error: $!";
print MAIL @contents;
close MAIL;

# 3. subroutines
# 3.1. get user name by uid
sub wanted {
    my @file_stat = stat($File::Find::name);
    next unless $File::Find::name =~ /^.+conf$/;
    printf("FILE: %-30s\tSIZE: %-5s\tCHANGE: %-5s\tCREAT: %-5s\tOWNER:
%-5s\tGROUP: %-5s\n",
            $File::Find::name,  # filename
            $file_stat[7],  # file size
            $file_stat[9],  # last change
            $file_stat[10],  # last change
            &get_user($file_stat[4]),  # owner
            &get_group($file_stat[5]),  # owner
        );
}
sub get_user {
    my $uid = @_[0];
    open FILE, "/etc/passwd" or die
        "Open file error: $!";
    foreach (<FILE>) {
        next unless /^.*:.*:${uid}:.*:.*:.*:.*$/;
        my @user_line = split /:/, $_;
        return $user_line[0];
    }
    close(FILE);
}
# 3.2. get group name by gid
sub get_group {
    my $gid = @_[0];
    open FILE, "/etc/group" or die
        "Open file error: $!";
    foreach (<FILE>) {
        next unless /^.*:.*:${gid}:.*$/;
        my @user_line = split /:/, $_;
        return $user_line[0];
    }
    close(FILE);
}


程序写的很烂。我意思是:
直接从find的输出内容中转到mail发送出去。上面这好象不行。

另外perl中有没有好用的根据uid查用户名的函数?
--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛“PerlChina 论坛”论坛。
 要在此论坛发帖,请发电子邮件到 [email protected]
 要退订此论坛,请发邮件至 [email protected]
 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---

回复