setperm
#!/usr/bin/env bash
# This script is used to set the permissions of the files in a given directory
__setperm() {
if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ -z "$1" ]; then
echo "Usage: setperm [user = $(whoami)] [path = .] [rwx = rwx]"
echo "Set the permissions of the files in the specified directory"
echo "If no path is specified, the permissions of the current directory will be changed"
return 1
fi
if [ $# -eq 0 ]; then
read -p "No arguments provided. Do you want to set the permissions of the current user ($(whoami)) on path to the current directory ($PWD) to rwx? (y/N): " confirm
if [[ "$confirm" != [yY] ]]; then
echo "Operation cancelled."
return 1
fi
fi
# NOTE: User is set to the current user by default
selected_user="${1}"
if [ -z "$selected_user" ]; then
selected_user="$(whoami)"
fi
# NOTE: Path is set to the current directory by default
selected_path="${2}"
if [ -z "$selected_path" ]; then
selected_path="${PWD}"
fi
if [ ! -e "$selected_path" ]; then
echo "The specified path '$selected_path' does not exist!"
return 1
fi
# NOTE: The permissions are set to rwx by default
selected_perms="${3}"
if [ -z "$selected_perms" ]; then
selected_perms="rwx"
fi
if [[ ! "$selected_perms" =~ ^[r-][w-][x-]$ ]]; then
echo "The specified permissions '$selected_perms' are not in the correct format (rwx)!"
return 1
fi
sudo setfacl -R -m "u:${selected_user}:${selected_perms}" "$selected_path"
sudo setfacl -R -m "g:${selected_user}:${selected_perms}" "$selected_path"
echo "Permissions set successfully for user '$selected_user' in path '$selected_path'"
}
__setperm "$@"