如果文件权限大于755,如何签入Perl?
对于unix文件,我想知道Group或World是否对该文件具有写入权限 我一直在思考这些问题:如果文件权限大于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
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)代码>更容易接受。