I wrote a simple script to set permissions for a file or folder for some embedded Linux devices as part of a larger setup script.
This method would be called on less than 20 files and folders. It seems to work correctly. For the Linux OS, the stat command is not available. Please let me know if anyone notices any issue or knows of a better way to set and check file and folder permissions.
Thanks.
# Runs 'chmod' command to set folder or file permissions.
# Uses 'find' command with permission parameter to verify
# the file or folder exists with the permissions that
# were set.
# Param1 = NumericPermission. Param2 = File/Folder path.
set_permissions()
{
local text
chmod $1 $2
text="$(find $2 -maxdepth 0 -perm $1)"
if [ -e $2 ]; then
if [ -n "$( echo $text | grep $2)" ]; then
PermissionResult=$PASSED
echo "Permissions set."
else
PermissionResult=$FAILED
printf " Permissions for '$2' not correctly set.\n" >> $SETUP_LOG
fi
else
printf " Path not found for '$2'.\n" >> $SETUP_LOG
fi
}
#Usage:
local file='/home/testfile'
printf "test file" > $file
set_permissions 777 $file