通过实例学习perl chmod函数的用法。
例1,
#!/bin/perl
#Format
#chmod(LIST);
#chmod LIST;
$ perl -e '$count=chmod 0755, "foo.p", "boo.p" ;print "$count files changed.n"'
例2,改变文件的权限
#!/usr/local/bin/perl -w
use Getopt::Long;
my $ret = GetOptions ("f|filename:s", "p|permission:s");
my $filename = $opt_f || die "Usage: $0 -f filename -p Permissionn";
my $newPerm = $opt_p || die "Usage: $0 -f filename -p Permissionn";
# Does the file exist?
if (! -e $filename)
{
print "The file $filename does not exist.n";
exit;
}
# Translate the string mode to an octal value
my $mode = oct($newPerm);
# Change the permissions of the file.
if ((chmod $mode, $filename) != 1)
{
print "Error: Could not change permissions on $filename : $!n";
}
附,The chmod Function (UNIX)