Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: check if file is executable by user/group/world?

by vinoth.ree (Parson)
on Aug 06, 2009 at 07:08 UTC ( #786331=note: print w/ replies, xml ) Need Help??


in reply to check if file is executable by user/group/world?

I assume that you want to check the permission of the file

Fcntl':mode' module contains all the constants and functions you need to extract the file type and permissions. S_IMODE is the function which helps you extract the permissions: $permissions = S_IMODE($mode);


Comment on Re: check if file is executable by user/group/world?
Re^2: check if file is executable by user/group/world?
by Utilitarian (Vicar) on Aug 06, 2009 at 07:57 UTC
    Just a quick clarification on the source of $mode in the ree's example above
    perl -e 'use Fcntl qw(:mode);$permissions=S_IMODE((stat("filename"))[2 +]);print $permissions,"\n";'

      Sorry!Utilitarian I just given the hint, and missed something,Below the code I just tried,

      use Data::Dumper; use Fcntl ':mode'; my $filename="pl_bak.pl"; my @array=lstat($filename); $permissions = sprintf "%04o", S_IMODE($array[2]); print "File permissions: $permissions\n";
      Update

      Removed a file type line.

Re^2: check if file is executable by user/group/world?
by XooR (Beadle) on Aug 06, 2009 at 08:00 UTC

    Use "use Fcntl ':mode';" to import S_I* constants. They are just bitmasks that masks bits from portions of files permissions. In Unix systems they are grouped into 4 groups of 3 bits. First three are for setuid(4), setgid(2), stickybit(1). Other three groups of bits are for user, owner group and others. Permissions are read(4), write(2) and execute(1).

    Permissions are often represented in octal form, so for this permission "rwxr-x-w-" you will have octal one 0752.

    I recommend you reading "perldoc -f stat" document, you have it all there. Except some of the stuff about inner working, which you could find in "info glibc" if you are working on some Linux system.

    Code from stat doc page:
    #!/usr/bin/env perl use Fcntl ':mode'; use strict; use warnings; my $mode = (stat($ARGV[0]))[2]; my $user_rwx = ($mode & S_IRWXU) >> 6; my $group_read = ($mode & S_IRGRP) >> 3; my $other_execute = $mode & S_IXOTH; printf "Permissions are %04o\n", S_IMODE($mode), "\n"; my $is_setuid = $mode & S_ISUID;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://786331]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2014-09-30 18:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (381 votes), past polls