#!/usr/bin/env dash match="/some/test/path/" default_name="root" uid_max="65534" read -p "Enter chmod user or UID (default: $default_name): " -r chuser read -p "Enter chmod group or UID (default: $default_name): " -r chgroup chuser="${chuser:-$default_name}" chgroup="${chgroup:-$default_name}" # result is a boolean 0 or 1; 1 means value out of permitted range iduser="$((chuser + 0 > uid_max))" 2>/dev/null # shellcheck disable=SC2181 if [ $? -ne 0 ]; then # Illegal Number; possibly a username if ! getent passwd "$chuser" >/dev/null; then >&2 printf "Error: '%s' does not exist\n" "$chuser" exit 1 fi fi if [ $iduser -eq 1 ]; then >&2 printf "Error: maximum user ID is %d\n" "$uid_max" exit 2 fi # result is a boolean 0 or 1; 1 means value out of permitted range idgroup="$((chgroup + 0 > uid_max))" 2>/dev/null # shellcheck disable=SC2181 if [ $? -ne 0 ]; then # Illegal Number; possibly a username if ! getent group "$chgroup" >/dev/null; then >&2 printf "Error: '%s' does not exist\n" "$chgroup" exit 3 fi fi if [ $idgroup -eq 1 ]; then >&2 printf "Error: maximum group ID is %d\n" "$uid_max" exit 4 fi # at this point both chuser and chgroup are either existing names or valid numeric IDs chmod --recursive u=rwX,g=rX,o-rwx "$(dirname "$match")" chown --recursive "$chuser:$chgroup" "$(dirname "$match")"