Categorized | Biocompute

Tags | ,

Perl chmod函数

Posted on 03 十一月 2009 by 柳城 ,阅读 346

学习Perl语言编程中……,打算边学边把Perl函数整理出来。

介绍:

chmod函数改变一列文件的权限。列表的第一个元素必须是数字模式。chmod函数返回成功改变了的文件的数目。如:

$cnt = chmod 0755, 'file1', 'file2';

会把$cnt设置为0,1,或2,具体多少取决于改变的文件的数目。

用法:

chmod LIST

例子:

 

   $cnt = chmod 0755, 'foo', 'bar';
    chmod 0755, @executables;

    $mode = '0644'; chmod $mode, 'foo';      
    # !!! sets mode to --w----r-T
    # 数字模式不能用引号!看起来是八进制数字的原因。

    $mode = '0644'; chmod oct($mode), 'foo'; # 用oct函数把八进制转换成一个数字
    $mode = 0644;   chmod $mode, 'foo';      # 不用引号就最好了!

如果返回错误,意味着你没有足够的权限修改文件模式。你可能既不是文件的所有者,也不是超级用户。用$!看看失败的实际原因是什么。

下面是典型的用法:

chmod(0755, @executables) == @executables
     or die "couldn't chmod some of @executables:$!";
如果你想知道是哪个文件不允许这样修改,使用如下所示的代码:

@cannot = grep {not chmod 0755, $_} 'file1', 'file2', 'file3';
die "$0:could not chmod @cannot\n" if @cannot;

此处使用grep函数选择列表里那些chmod函数对之操作失败的元素。

转载请注明 : 来源于 Perl chmod函数 | 柳城

赞助商

Leave a Reply

广告招租

[强] [握手] [可爱] [ok] [呲牙] :) [偷笑] [流泪] [疑问] [亲亲] [擦汗] [得意] [衰] [可怜] [抱拳] [坏笑] more »

无觅相关文章插件,快速提升流量