?
Solved

Shell script to find out owner, group permissions of file

Posted on 2006-06-01
3
Medium Priority
?
1,312 Views
Last Modified: 2012-06-22
Hi,
How do you write a shell script which takes an argument and determine what permissions the owner and group has and outputs the result in the following format

                                                                        READ            WRITE            EXECUTE

OWNER            User.Name                   YES                     YES                       NO
GROUP            GroupName                                  YES                     NO                         NO

Thanks
0
Comment
Question by:sixpack434
  • 2
3 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 16808792
Use stat on the files you are interested in or parse the output of ls -l on that file, if you have a printf in reach then use it. Or alternative use the templates from Perl.

0
 
LVL 24

Accepted Solution

by:
fridom earned 500 total points
ID: 16808914
Here's a quite nice solution:
#!/usr/bin/zsh
#-xv

ls -l $1 | awk '

   function build_perm_string(start, end, perms) {
     j = 0;
     for (i = start; i < end; i++){
       sstr = substr(perms,i,1)
       # print sstr
       if (sstr  == "-") {
         a_perm[j] = "NO"
       } else {
         a_perm[j] = "YES"
       }
       j++
     }
     fmt_perms = sprintf("%8s %8s %8s", a_perm[0], a_perm[1], a_perm[2]);
     return fmt_perms;
  }

{
  perms=$1
  user_name=$3
  group_name=$4
}

END{
 perm_line = sprintf("%8s %8s %8s", "READ", "WRITE", "EXECUTE")
 printf("%20s %s\n", " ", perm_line)
 printf("%-6s%14s %s\n", "User:", user_name, build_perm_string(2,5, perms))
 printf("%6s%14s %s\n", "Group:", group_name, build_perm_string(5,8, perms))
 printf("%6s%14s %s\n", "Other:", "", build_perm_string(8,11, perms))
}
'

Friedrich
0
 
LVL 7

Expert Comment

by:wnross
ID: 16809024
You can use perl and stat to get the filemode information,
you can then mask out individual permissions:
so $mode & 0400 == user readable.

All of these permissions are listed as symbolic constants in the Fcntl package, the relevant ones are:
    S_IRUSR S_IWUSR S_IXUSR
    S_IRGRP S_IWGRP S_IXGRP
    S_IROTH S_IWOTH S_IXOTH

Here's a code sample which would be trivial to finish: Save as perms.pl and try the following:
chmod +x perms.pl
./perms.pl /tmp
./perms.pl /var/adm
./perms.pl /home/myusername

--------------------- CUT HERE ---------------------
#!/usr/bin/perl
use Fcntl ':mode';

$file = <>;
chomp $file;

($mode,$uid,$gid) = (stat($file))[2,4,5];

$mode = $mode & 07777;
print "Mode $mode\nUID $uid\nGID $gid\n";
$usr = getpwuid($uid);
$grp = getgrgid($gid);

print "\n mode $mode " . S_IRUSR . "\n";
$u[4]   = ($mode & S_IRUSR) >> 6;
$u[2]   = ($mode & S_IWUSR) >> 6;
$u[1]   = ($mode & S_IXUSR) >> 6;
$g[4]   = ($mode & S_IRGRP) >> 3;
$o[1]   =  $mode & S_IXOTH;

print << "RESULTS"
                        READ     WRITE    EXECUTE
OWNER            $usr   $u[4]       $u[2]      $u[1]
GROUP            $grp   $g[4]       $g[2]      $g[1]
OTHER                   $o[4]       $o[2]      $o[1]

RESULTS

--------------------- CUT HERE ---------------------

Cheers,
-Bill
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Simple Linear Regression

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question