Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help: Filemask problem

Reply
Thread Tools

Help: Filemask problem

 
 
Amy Lee
Guest
Posts: n/a
 
      10-14-2007
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
 
Reply With Quote
 
 
 
 
Amy Lee
Guest
Posts: n/a
 
      10-14-2007
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 <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
 
Reply With Quote
 
 
 
 
Martien Verbruggen
Guest
Posts: n/a
 
      10-14-2007
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.
|
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-14-2007
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/"
 
Reply With Quote
 
Amy Lee
Guest
Posts: n/a
 
      10-14-2007
Thank you very much~

I've solved this problem, thanks really~

Regards,

Amy Lee
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments