Help: Filemask problem

Discussion in 'Perl Misc' started by Amy Lee, Oct 14, 2007.

  1. Amy Lee

    Amy Lee Guest

    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
     
    Amy Lee, Oct 14, 2007
    #1
    1. Advertising

  2. Amy Lee

    Amy Lee Guest

    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
     
    Amy Lee, Oct 14, 2007
    #2
    1. Advertising

  3. 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.
    |
     
    Martien Verbruggen, Oct 14, 2007
    #3
  4. 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/"
     
    Tad McClellan, Oct 14, 2007
    #4
  5. Amy Lee

    Amy Lee Guest

    Thank you very much~

    I've solved this problem, thanks really~

    Regards,

    Amy Lee
     
    Amy Lee, Oct 14, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dHJlbGxvdzQyMg==?=

    HELP! HELP! HELP! Opening Web Application Project Error

    =?Utf-8?B?dHJlbGxvdzQyMg==?=, Feb 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    795
    =?Utf-8?B?dHJlbGxvdzQyMg==?=
    Feb 20, 2004
  2. Harvey
    Replies:
    0
    Views:
    758
    Harvey
    Jul 16, 2004
  3. Harvey
    Replies:
    1
    Views:
    883
    Daniel
    Jul 16, 2004
  4. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    562
    Bullschmidt
    May 11, 2004
  5. George Mpouras

    filemask to regex

    George Mpouras, Aug 24, 2013, in forum: Perl Misc
    Replies:
    10
    Views:
    232
    Rainer Weikusat
    Aug 27, 2013
Loading...

Share This Page