Help: Filemask problem

Help: Filemask problem

am 14.10.2007 13:44:35 von Amy Lee

Hello,

I write a perl script to show the file mask like -rwxr-xr-x is 0755, but
when I run my script, it shows 835 in this mode. I don't know why.

There's my code:

#!/usr/bin/perl -w

if (@ARGV == 0)
{
die "Usage: filemask.pl \n";
}

if (@ARGV != 0)
{
foreach $file (@ARGV)
{
unless (-e $file)
{
print "***Error: $file dose not exist.\n";
next;
}
unless (-r $file)
{
print "***Error: Cannot read $file.\n";
next;
}
my($mode) = stat($file);
print "$file ==> $mode\n";
}
}

Could you tell me how to solve this?

Thank you very much~

Regards,

Amy Lee

Re: Help: Filemask problem

am 14.10.2007 13:55:35 von Amy Lee

On Sun, 14 Oct 2007 19:44:35 +0800, Amy Lee wrote:

> Hello,
>
> I write a perl script to show the file mask like -rwxr-xr-x is 0755, but
> when I run my script, it shows 835 in this mode. I don't know why.
>
> There's my code:
>
> #!/usr/bin/perl -w
>
> if (@ARGV == 0)
> {
> die "Usage: filemask.pl \n";
> }
> }
> if (@ARGV != 0)
> {
> foreach $file (@ARGV)
> {
> unless (-e $file)
> {
> print "***Error: $file dose not exist.\n"; next;
> }
> unless (-r $file)
> {
> print "***Error: Cannot read $file.\n"; next;
> }
> my($mode) = stat($file);
> print "$file ==> $mode\n";
> }
> }
> }
> Could you tell me how to solve this?
>
> Thank you very much~
>
> Regards,
>
> Amy Lee

I change this part:

my($mode) = stat($file); to

my($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime,
$ctime, $blksize, $blocks) = stat($file);

However, it's useless, what happened?

Thanks,

Amy Lee

Re: Help: Filemask problem

am 14.10.2007 14:07:13 von Martien Verbruggen

On Sun, 14 Oct 2007 19:55:35 +0800,
Amy Lee wrote:
> On Sun, 14 Oct 2007 19:44:35 +0800, Amy Lee wrote:
>
>> Hello,
>>
>> I write a perl script to show the file mask like -rwxr-xr-x is 0755, but
>> when I run my script, it shows 835 in this mode. I don't know why.

[snip]

>> my($mode) = stat($file);
>> print "$file ==> $mode\n";

> I change this part:
>
> my($mode) = stat($file); to
>
> my($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime,
> $ctime, $blksize, $blocks) = stat($file);
>
> However, it's useless, what happened?

Print the mode in octal. The permissions you refer to above are octal
numbers, but you print them in decimal.

#!/usr/bin/perl
use warnings;
use strict;

for my $f (@ARGV)
{
my ($mode) = (stat $f)[2];
printf "$f: %o\n", $mode;
}

Martien
--
|
Martien Verbruggen |
| The gene pool could use a little chlorine.
|

Re: Help: Filemask problem

am 14.10.2007 14:30:15 von Tad McClellan

Amy Lee wrote:

> I write a perl script to show the file mask like -rwxr-xr-x is 0755,


> There's my code:
>
> #!/usr/bin/perl -w


[ snip 20 lines that are not needed to illustrate the problem ]


> my($mode) = stat($file);
> print "$file ==> $mode\n";


> Could you tell me how to solve this?


As with most of the questions that you post here, you can solve it
by reading the documentation for the function that you are using:

perldoc -f stat

...
Because the mode contains both the file type and its permissions,
you should mask off the file type portion and (s)printf using a
"%o" if you want to see the real permissions.

$mode = (stat($filename))[2];
printf "Permissions are %04o\n", $mode & 07777;


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Help: Filemask problem

am 14.10.2007 15:34:58 von Amy Lee

Thank you very much~

I've solved this problem, thanks really~

Regards,

Amy Lee