Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果文件权限大于755,如何签入Perl?_Perl_File Permissions - Fatal编程技术网

如果文件权限大于755,如何签入Perl?

如果文件权限大于755,如何签入Perl?,perl,file-permissions,Perl,File Permissions,对于unix文件,我想知道Group或World是否对该文件具有写入权限 我一直在思考这些问题: my $fpath = "orion.properties"; my $info = stat($fpath) ; my $retMode = $info->mode; $retMode = $retMode & 0777; if(($retMode & 006)) { # Code comes here if World has r/w/x on the fi

对于unix文件,我想知道Group或World是否对该文件具有写入权限

我一直在思考这些问题:

my $fpath   = "orion.properties";
my $info    = stat($fpath) ;
my $retMode = $info->mode;
$retMode = $retMode & 0777;

if(($retMode & 006)) {
  # Code comes here if World has r/w/x on the file
} 
谢谢。

#/usr/bin/perl
#!/usr/bin/perl

use warnings;
use strict;

chomp (my $filename = <STDIN>);

my $lsOutput = `ls -l $filename`;

my @fields = split (/ /,$lsOutput);

my @per = split (//,$fields[0]);

print "group has write permission \n" if ($per[5] eq 'w');

print "world has write permission" if ($per[8] eq 'w');
使用警告; 严格使用; chomp(我的$filename=); my$lsOutput=`ls-l$filename`; my@fields=拆分(/,$lsOutput); 我的@per=split(/,$fields[0]); 如果($per[5]eq'w'),则打印“组具有写入权限”; 如果($per[8]eq'w'),打印“world拥有写入权限”;
您的提案已接近尾声-的用法有点不正确(但经过再三考虑,您必须使用;如果您的代码完整,它会有所帮助),掩码常量有错误,并且注释有点不尽如人意:

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

my $fpath   = "orion.properties";
my $info    = stat($fpath);
my $retMode = $info->mode;
$retMode = $retMode & 0777;

if ($retMode & 002) {
    # Code comes here if World has write permission on the file
}     
if ($retMode & 020) {
    # Code comes here if Group has write permission on the file
}
if ($retMode & 022) {
    # Code comes here if Group or World (or both) has write permission on the file
}
if ($retMode & 007) {
    # Code comes here if World has read, write *or* execute permission on the file
} 
if ($retMode & 006) {
    # Code comes here if World has read or write permission on the file
} 
if (($retMode & 007) == 007) {
    # Code comes here if World has read, write *and* execute permission on the file
} 
if (($retMode & 006) == 006) {
    # Code comes here if World has read *and* write permission on the file
}
if (($retMode & 022) == 022) {
    # Code comes here if Group *and* World both have write permission on the file
}

问题标题中的术语“如果文件权限大于755,如何签入Perl?”?i、 e.组/世界有写入权限“有点可疑

该文件可能具有权限022(或者更合理地说是622),这将包括组和世界写入权限,但这两个值都不能合理地声明为“大于755”

我发现一组有用的概念是:

  • Set bits—权限字段中必须为1的位
  • 重置位-权限字段中必须为0的位
  • 不在乎位-可以设置或重置的位
例如,对于数据文件,我可能需要:

  • 设置0644(所有者可以读写;组和其他可以读)
  • 重置0133(所有者无法执行-它是数据文件;组和其他无法写入或执行)
更可能的是,对于数据文件,我可能需要:

  • 设置0400(所有者必须能够读取)
  • 重置0133(没有人可以执行;组和其他不能写入)
  • 不在乎0244(不管所有者是否可以写入;不管组或其他人是否可以读取)
目录略有不同:执行权限意味着您可以将该目录设置为当前目录,或者如果您知道目录中的文件名,则可以访问该目录中的文件,而读取权限意味着您可以找到目录中的文件,但如果没有执行权限,您也无法访问这些文件。因此,您可能有:

  • 设置0500(所有者必须能够读取和使用目录中的文件)
  • 重置0022(组和其他人不能修改目录-删除或添加文件)
  • 不关心0255(不关心用户是否可以创建文件;不关心组或其他用户是否可以列出或使用文件)

请注意,设置位和重置位必须是不相交的(
($set&$rst)==0)
),位的总和将始终为0777;“不在乎”位可以从
0777&($set |$rst)
计算出来。

解析
ls
的输出不可靠;这也不是有效的或必要的。Perl内置了必要的功能。请使用模式常量(
S_I*
)而不是幻数。对于像我这样的老顽固来说,常量比
Fcntl
中的字母汤更容易读取。单独来看,这些常量可读性更高,但与
0755
相比,
S|IRWXU | S|IRGRP | S|IXGRP | S|IROTH | S|IXOTH
更难读取。然而,你可能是对的,使用字母汤是“更好的风格”。我想我可以定义一个方便的常数:
使用常数S|I755=>(S|IRWXU | S|IRGRP | S|IXGRP | S|IROTH | S|IXOTH)?(是的,这是一个笑话。)也许
使用常量S_RWXR_XR_X=>(S|IRWXU | S|IRGRP | S|IROTH | S|IXOTH)更容易接受。