Looking for a macro that converts unix file permissions string to a number

Accepted Solution Solved
Reply
Contributor
Posts: 71
Accepted Solution

Looking for a macro that converts unix file permissions string to a number

Looking for a macro that converts unix file permissions string to a number

 

Like -

-rwxr-xr-x    should return 755

-rwxrwxr-x   should retrun 775

-rwx------     should return 700

 

Has anyone something like this readily available?

 

Rgds, Anil

 


Accepted Solutions
Solution
‎01-18-2017 04:07 AM
Super User
Posts: 1,244

Re: Looking for a macro that converts unix file permissions string to a number

data perm;
input perm $10.;
array vn v1-v9;
do i=1 to 9;
j=i+1;
x=substr(perm,j,1);
if x='-' then vn(i) =0; else
if x='r' then vn(i) =4; else
if x='w' then vn(i) =2; else
if x='x' then vn(i) =1;
end;
code=(v1+v2+v3)*100+(v4+v5+v6)*10+v7+v8+v9;
keep perm code;
cards;
-rwxr-xr-x
-rwxrwxr-x
-rwx------
; run;

View solution in original post


All Replies
Solution
‎01-18-2017 04:07 AM
Super User
Posts: 1,244

Re: Looking for a macro that converts unix file permissions string to a number

data perm;
input perm $10.;
array vn v1-v9;
do i=1 to 9;
j=i+1;
x=substr(perm,j,1);
if x='-' then vn(i) =0; else
if x='r' then vn(i) =4; else
if x='w' then vn(i) =2; else
if x='x' then vn(i) =1;
end;
code=(v1+v2+v3)*100+(v4+v5+v6)*10+v7+v8+v9;
keep perm code;
cards;
-rwxr-xr-x
-rwxrwxr-x
-rwx------
; run;

Contributor
Posts: 71

Re: Looking for a macro that converts unix file permissions string to a number

Hi Shmuel,

How to handle Sticky Bits, UID & GID's with this code? Thanks your guidance.
Esteemed Advisor
Posts: 6,698

Re: Looking for a macro that converts unix file permissions string to a number

[ Edited ]

The sticky bits occupy a different position in the octal value:

data perm (keep=perm perms_oct);
input perm $10.;
perms_oct = 0;
do i = 0 to 2;
  read = (substr(perm,i*3+2,1)='r');
  write = (substr(perm,i*3+3,1)='w');
  exec = (substr(perm,i*3+4,1) in ('x','s'));
  setb = (substr(perm,i*3+4,1) in ('S','s'));
  i1 = abs(i - 2);
  oct = read * 4 + write * 2 + exec * 1;
  if oct ne 0 then perms_oct = perms_oct + oct * 8 ** i1;
  if setb ne 0 then perms_oct = perms_oct + (((setb*2) ** i) * 512);
end;
format perms_oct octal4.;
cards;
-rwxr-xr-x
-rwxrwxr-x
-rwx------
-rwsr-sr-x
---------S
;
run; proc print data=perm noobs; run;

result looks like this:

              perms_
   perm        oct

-rwxr-xr-x     0755 
-rwxrwxr-x     0775 
-rwx------     0700 
-rwsr-sr-x     3755 
---------S     4000 

 Edit: slightly changed my code to accomodate the fact that the SAS ** operator doesn't like 0 ** 0.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Esteemed Advisor
Posts: 6,698

Re: Looking for a macro that converts unix file permissions string to a number

Use the stat command for files:

stat -c "%a %n" filename

on Linux. It delivers the permissions in numerical form, followed by the filename.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 71

Re: Looking for a macro that converts unix file permissions string to a number

Thank you so much. This small code really awesome.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 155 views
  • 1 like
  • 3 in conversation