Excellent, John. Didn't know that Perl only deals with decimal numbers. Funny thing was I did think that it could be a hex, so converted it to binary and done an AND, but didn't get the desired result. Wonder why I didn't give a thought that it could be decimal!
Regards, Akhthar Parvez K http://Tips.SysAdminGUIDE.COM UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity - Dennie Richie On Saturday 03 Apr 2010, John W. Krahn wrote: > Akhthar Parvez K wrote: > > Hi all, > > Hello, > > > I have been trying to get the mode of a directory with the stat > > function in perl (currently using the one from File::stat) with no > > success so far. > > It looks like it is working to me. > > > Basically I want to check if a given directory is > > having 1777 permission (like /tmp) and I was thinking about storing > > the mode of the directory in a variable and check that variable for > > the value 1777. > > In Perl 1777 is a decimal value but you want the octal value 01777 > instead (for example: $mode == 01777). Perl internally converts the > octal representation of 01777 to the decimal number 1023: > > $ perl -le'print 01777' > 1023 > > > > With this setup, however I'm unable to get the actual octal mode of > > the directory stored into the variable. > > That is because perl only deals with decimal numbers. To convert the > decimal number to its octal representation you have to use > printf/sprintf (as you have done below.) > > > > I'm getting a weird value and > > I can print the actual value by masking off the type using printf > > "%40o", but here I don't have to print the perm, want to get it to use > > it in an if condition. > > The "weird value" is the actual value and the "actual value" is just the > octal representation of the actual decimal value. > > > > #!/usr/bin/perl > > use File::stat; > > my $dir=$ARGV[0]; > > $st = stat($dir) or die "No $dir: $!"; > > my $stat_mode = $st->mode; > > my $mode = $stat_mode & 07777; > > print "stat_mode: $stat_mode\tmode: $mode\n"; > > printf "Actual value:%04o\n",$mode; > > > > But, as you know, $mode is not storing the actual mode value. > > Yes it is. It is just storing it as a decimal number and not as its > octal representation. > > > > If the > > actual mode of the directory is 0755, the following is the output: > > stat_mode: 16877 mode: 493 > > Actual value:0755 > > > > Could anyone tell me how can I check if the given directory is having > > 1777 perm mode? > > $ perl -le' > my $mode = ( stat "/tmp" )[ 2 ] & 07777; > print "/tmp mode is ", $mode == 01777 ? "" : "NOT ", "equal to 01777" > ' > /tmp mode is equal to 01777 > $ perl -le' > my $mode = ( stat "/usr" )[ 2 ] & 07777; > print "/usr mode is ", $mode == 01777 ? "" : "NOT ", "equal to 01777" > ' > /usr mode is NOT equal to 01777 > > > If you are on *nix then check the man page for the different values that > mode can contain: > > man 2 stat > > [ SNIP ] > > The following flags are defined for the st_mode field: > > S_IFMT 0170000 bit mask for the file type bit fields > S_IFSOCK 0140000 socket > S_IFLNK 0120000 symbolic link > S_IFREG 0100000 regular file > S_IFBLK 0060000 block device > S_IFDIR 0040000 directory > S_IFCHR 0020000 character device > S_IFIFO 0010000 FIFO > S_ISUID 0004000 set UID bit > S_ISGID 0002000 set-group-ID bit (see below) > S_ISVTX 0001000 sticky bit (see below) > S_IRWXU 00700 mask for file owner permissions > S_IRUSR 00400 owner has read permission > S_IWUSR 00200 owner has write permission > S_IXUSR 00100 owner has execute permission > S_IRWXG 00070 mask for group permissions > S_IRGRP 00040 group has read permission > S_IWGRP 00020 group has write permission > S_IXGRP 00010 group has execute permission > S_IRWXO 00007 mask for permissions for others (not in group) > S_IROTH 00004 others have read permission > S_IWOTH 00002 others have write permission > S_IXOTH 00001 others have execute permission > > > > John > -- > The programmer is fighting against the two most > destructive forces in the universe: entropy and > human stupidity. -- Damian Conway > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/