What type of access? Read? Write? Both? Try the -x, -r, -w operators:
$ perl -e 'print "Can read\n" if -r "/home"'
$ perl -e 'print "Can write\n" if -w "/home"'
$ perl -e 'print "Can execute (or cd into if dir)\n" if -x "/home"'
-derby | [reply] [d/l] |
how can i combine all there in one go instead of in three ways thanks
| [reply] |
THANK YOU SOOOOOOOOO MUCH I LOVE THE HELP YOU GAVE ME
| [reply] |
The example given by draconis only provides the "owner/group/other" mode flags for the file in question. It looks like the OP was asking how to learn what sort of access is available to the person running the perl script.
In order to do that, you would need to combine the mode field from stat with knowledge of the current user's group membership(s) and whether the current user happens to be the owner of the file, so you'll know which set of bits from the mode flag are relevant for determining file access.
A better way for the task at hand would be:
($r,$w,$x) = (-r $path, -w _, -x _);
print "Access: read $r, write $w, execute (search dir) $x\n";
A few notes:
- using underscore as the argument for "-w" and "-x" tells perl to use the same "stat" data obtained by "-r"
- as written above, any permission not granted will be "undef", so the printed message in this case would look like "read 1, write , execute (search dir) 1" when there is no write permission
- contrary to derby's comment, "execute" permission on a directory does not control whether or not you can "cd" into that directory; it does determine whether you can "search" the directory contents -- i.e. you need execute permission to run "ls" with no args, or "find", or do any sort of operation using "*" or "?" as part of a file name within the directory.
| [reply] [d/l] |
$filemode=(stat($filename))[2];
#check permissions here with slick XOR
Cheers | [reply] [d/l] |
i am very new with perl and get a lot of syntax trouble if possible could u sum it up for me ... if it's not too much trouble for you
Thanks .. I am reading the perldoc and they have an example in it .. but it's kind of confusing ..and i need the permissions for the directories not files .. thanks again
| [reply] |
You could try stat or even File::Stat
--
Can't never could do anything, so give me and inch, I'll make it a mile. | [reply] |
If you have the camel book see page 801. If not, here is the code:
$mode = (stat($filename))[2];
printf "permissions are %04o\n", $mode &07777
This will send back to you the usual UNIX-style permissions - once you have them - you can then interrogate them as you wish. | [reply] [d/l] |
how to factor in the sticky bit while getting the permissions?
| [reply] |
| [reply] |
could show me a script that would do that , if Possible
thank you very much
| [reply] |