sixpack434
asked on
Shell script to find out owner, group permissions of file
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
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
OWNER User.Name YES YES NO
GROUP GroupName
Thanks
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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