Operating System - HP-UX
1834137 Members
2388 Online
110064 Solutions
New Discussion

Re: Perl File::stat question

 
SOLVED
Go to solution
David_246
Trusted Contributor

Perl File::stat question

Hi,

I am strugling with a perl question again.

I need to find out the permissions in digits using File::stat. It works using printf, but not by storing it into a var :

use File::stat;
printf "$file has permissions %04o ", $sb->mode & 07777;

But how do I get the correct value into $f_mode ??

my $f_mode = $sb->mode & 07777 ;

Does not work!
Any help will be much apreciated !!

Regs David
@yourservice
2 REPLIES 2
Steven Gillard_2
Honored Contributor
Solution

Re: Perl File::stat question

As long as you use printf to format the $f_mode variable as octal when you print it out you should be fine (otherwise you get the decimal value). If you want it as a string try sprintf, eg:

my $f_mode = sprintf("%04o", $sb->mode & 07777);

Regards,
Steve
Rodney Hills
Honored Contributor

Re: Perl File::stat question

It kind of depends on what you plan to do with $f_mode.

If you are going to some tests using "masking", then what you have is correct.

my $f_mode = $sb->mode & 07777;
if ($f_mode & 020) { print "We have group write here!\n"; }

If you want to manipulate the permissions as "text" representing the octal values, you can use sprintf.

my $f_mode = sprintf("%04o",$sb->mode & 07777);
my $grp=substr($f_mode,2,1);
# look for values with group write bit set
if ($grp eq "2" or $grp eq "3" or $grp eq "6" or $grp eq "7") { print "We have group write here!\n"; }

HTH

-- Rod Hills
There be dragons...