Testing for Permissions

by mike on November 15, 2011

Test Permissions
This script tests for the SGID permissions on a directory.  Remember SGID changes the effective user so that it elevates a user’s permissions for this directory.  The permissions are 2770.  The advantage for the sales directory is that all files created will take on the group ownership so files can be shared easily.

The script looks for the directory to verify it exists and then checks permissions with the “-perm” option with find.  This test then provides a success or no success which is evaluated and sent to the administrator.

#!/bin/bash
# Test for SGID on /opt/sales
ADMIN=root
FILE=/opt/sales
find $FILE -perm 2770 | grep sales
if [ "$?" -ne "0" ]
then
echo "Bad Permissions" | mail -s "Permissions not correct on Sales  $(date)" $ADMIN; echo "Bad Permissions"
else
echo "SGID Set" | mail -s "SGID set on Sales $(date)" $ADMIN; echo "SGID set on Sales"
fi
exit 0

This script could have been written differently with this line to test for permissions:

if  [ -g /opt/sales ];

This would replace the find command and use the “-g” option in the test.  This is cleaner code so probably a better option.  You would need to exchange the then…else clauses.

{ 1 comment }

Scott November 15, 2011 at 3:05 pm

You can also use the stat command, which will give you the information you want using format specifiers similar to the printf command without invoking a file system search.

:wq

Comments on this entry are closed.

Previous post:

Next post: