Skip to content

Instantly share code, notes, and snippets.

@lgaggini
Last active June 29, 2020 13:59
Show Gist options
  • Save lgaggini/d2257294b8abb54d905fb992fd1f7143 to your computer and use it in GitHub Desktop.
Save lgaggini/d2257294b8abb54d905fb992fd1f7143 to your computer and use it in GitHub Desktop.
Check if the target path has the expected permissions
#! /bin/bash
# Author: Lorenzo Gaggini - [email protected]
# Check if a the target path has the expected permissions
#
# Usage: ./check_file_perms.sh -t target_path -p expected_permissions
function usage()
{
echo "Usage:"
echo "./check_file_perms.sh -t target_path -p expected_permissions"
echo "-t (required): path to check for permissions"
echo "-p (required): expected octal permssisions for target_path"
}
while getopts ":ht:p:" opt; do
case ${opt} in
h ) usage && exit 0;;
t ) target_path=$OPTARG;;
p ) exp_perms=$OPTARG;;
\?|*) usage && exit 1;;
esac
done
if [ ! -d "${target_path}" ]; then
echo "${target_path} doesnt exists"
exit 1
fi
if [[ ! ${exp_perms} =~ ^[0-1][0-7]{3}$ ]]; then
echo "${exp_perms} is not a valid octal permissions"
exit 1
fi
current_perms=$(stat -c "%a" "${target_path}")
if [ "${exp_perms}" = "${current_perms}" ]; then
echo "OK, ${target_path} has the expected permissions: ${exp_perms}"
exit 0
else
echo "KO, ${target_path} has unexpected permissions: ${current_perms}, should be ${exp_perms}"
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment