Help: Filemask problem

A

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 <filename(s)>\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
 
A

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 <filename(s)>\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
 
M

Martien Verbruggen

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
 
T

Tad McClellan

Amy Lee said:
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top