2

我需要编写一个代码,以便它可以验证 DIRECTORY 权限。一直在“谷歌”周围,在我看来使用 File::stat 可能符合我的需求。但是,当我尝试如下代码时,它并没有给出我认为的结果。我正在寻找该目录是否没有读取和执行权限,它将显示目录名称。有人可以帮忙吗?下面是我从网上找到的代码行,尝试过但似乎没有给出一个有利的输出。

#! /usr/bin/perl

use strict ;
use warnings ;
use File::stat ;

my $path = "/nfs/ch/test_dir" ;

opendir (DH, $path) || die "Fail to open dir:$!\n" ; ;
my @dir = readdir (DH) ;
closedir (DH) ;

foreach my $dir (@dir) {
 print "DIR: $dir\n" ;
 $dirmode = (stat($dir)) [2] ;
 printf "Permissions are %04o\n", $dirmode & 07777;
 print "DIRmode= $dirmode\n" ;
 }

输出如下:- DIR: groupA Permissions are 0000 DIRmode= DIR: groupB Permissions are 0000 DIRmode=

我实际上正在寻找可以输出目录名称的代码,当它发现搜索路径中的目录没有 GROUP 和 WORLD 权限以供读取和执行时。先谢谢了。

4

3 回答 3

1
#! /usr/bin/perl

use strict ;
use warnings ;
use File::stat ;
use Fcntl ':mode';

my $path = "/nfs/ch/test_dir" ;

opendir (DH, $path) || die "Fail to open dir:$!\n" ; ;
my @dir = readdir (DH) ;
closedir (DH) ;

foreach my $dir (@dir) {
 print "DIR: $dir\n" ;
 $dirmode = (stat($dir)) [2] ;
 printf "Permissions are %04o\n", $dirmode & 07777;
 $group_read = ($dirmode & S_IRGRP) >> 3; // group read permission
 $group_exe = ($dirmode & S_IXGRP) >> 3; // group execute permission 
print "group read = $group_read\n" ; // 0 for not set
print "group execute = group_exe\n" ; // 0 for not set
 }
于 2013-06-25T05:37:49.667 回答
1

我会使用File::Findand stat

#!/usr/bin/perl

use warnings;
use strict;

use File::Find;

my $full_path = "/Users/chrisblack/Misc";

find(\&wanted, $full_path);

sub wanted {
    if (! -e || ! -d) {
        return;
    }

    my $mode = (stat($File::Find::name))[2];
    $mode = $mode & 0777;
    if(($mode & 060) == 060 || ($mode & 070) == 070) {
        print $File::Find::name, " has read and write perms\n";
    }
}

这是一个测试运行:

$ ls -l
drwxrwxrwx  2 chrisblack  staff   68 Jun 24 22:40 a_dir
drwxr--r-x  2 chrisblack  staff   68 Jun 24 22:26 b_dir
drwx--xr-x  2 chrisblack  staff   68 Jun 24 22:26 c_dir
drwxrw-r-x  2 chrisblack  staff   68 Jun 24 22:26 d_dir
-rwxr--r--  1 chrisblack  staff  395 Jun 24 22:53 test.pl
-rwxrwx---  1 chrisblack  staff  191 May 17 08:36 test.py
$ ./test.pl
/Users/chrisblack/Misc/a_dir has read and write perms
/Users/chrisblack/Misc/d_dir has read and write perms
于 2013-06-25T05:56:57.920 回答
0

适合我的 Perl 代码。要检查目录是否具有“写入”权限,

open(FH, ">check_write.txt") 
or die("Cannot write. Check write permission!");
close(FH);
system "rm check_write.txt";
于 2016-04-28T03:42:47.240 回答