Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to store octal permission of a file in variable? 2

Status
Not open for further replies.

J1mbo

Programmer
Dec 12, 2002
93
US
How can i capture the octal permission value of a file to a variable in perl?

example:

-rwxr--r-- 1 root other 38 Jan 31 12:43 test1

I need a variable that has a value of '744'

so i can use it later. Thanks,

jim
 
Code:
#!/usr/bin/perl

# get filename from arguments
my $filename = shift @ARGV; 

# use the stat() function to build a file attribute array
my $mode = (stat($filename))[2];

# print it out in octal
printf "mode is %04o\n", $mode & 07777;

# alternatively, convert $mode to octal with Bit::Vector
use Bit::Vector;
my $vec = Bit::Vector->new_Dec(16, $mode);
$mode = reverse join('', $vec->Chunk_List_Read(3)); 
$mode =~ s/^(\d*)(\d{4})$/$2/;
my $type = $1;
print qq~mode is $mode, type is $type\n~;
Sincerely,

Tom Anderson
Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top