# 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;

 这里有问题,open MAIL, "> |mail -s xxx [email protected]" or
die中不需要>吧,MAIL在这里是进程句柄,不需要像使用文件句柄那样使用
>表示重定向。



2009/1/8 joe jiang <[email protected]>

>
> 查用户名 getpwnam(0) => 'root'
> 查组名 getgrnam(0) => 'root'
>
> On 四, 2009-01-08 at 02:14 -0800, e280s wrote:
> > 点错了出现了两遍。谢谢,我再实验下。
> >
> > On Jan 8, 6:11 pm, Changying Li <[email protected]> wrote:
> > > 1. 不需要发两遍吧
> > > 2. 多看看File::Find文档,你的用法有误
> > >
> > > e280s <[email protected]> writes:
> > > > #!/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);
> > >
> > > 这里...@contents你能得到啥东西了? find不能这样用,你应该...@contents这个数组
> > > 在wantted里操作,然后再print 出来。
> > > 比如
> > > my @contents;
> > > sub wanted {
> > > push @contets, $_;
> > >
> > >
> > >
> > >
> > >
> > > }
> > > > 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查用户名的函数?
> > >
> > > --
> > >
> > > Thanks & Regards
> > >
> > > Changying Li- Hide quoted text -
> > >
> > > - Show quoted text -
> > >
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛“PerlChina 论坛”论坛。
 要在此论坛发帖,请发电子邮件到 [email protected]
 要退订此论坛,请发邮件至 [email protected]
 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---

回复