Fixed add-user command

This commit is contained in:
Brian Zalewski 2023-11-28 01:03:00 +00:00
parent 097396d750
commit 9a70a9be99
2 changed files with 32 additions and 41 deletions

View file

@ -161,4 +161,9 @@ if [ -d /Applications ] && [ -d System ]; then
brew autoupdate start --cleanup --greedy --upgrade brew autoupdate start --cleanup --greedy --upgrade
fi fi
fi fi
### Scrub ~/.viminfo
if [ -f "$HOME/.viminfo" ]; then
logg info 'Removing ~/.viminfo' && sudo rm -f "$HOME/.vimino"
fi
{{ end -}} {{ end -}}

View file

@ -7,8 +7,6 @@
# there is already a user / group with the name present on the system before running # there is already a user / group with the name present on the system before running
# any code. On macOS, it assigns the user an ID that equal to the maximum user ID present # any code. On macOS, it assigns the user an ID that equal to the maximum user ID present
# on the system plus one. # on the system plus one.
#
# This script was generated with ChatGPT.
# Check if the script is being run as root # Check if the script is being run as root
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
@ -40,50 +38,36 @@ if [[ "$(uname)" == "Darwin" ]]; then
echo -e "\e[96mCreating group and user ${GROUP} on macOS...\e[0m" echo -e "\e[96mCreating group and user ${GROUP} on macOS...\e[0m"
fi fi
# Check if the group already exists # Ensure group exists
if ! sudo dscl . read /Groups/"${GROUP}" >/dev/null 2>&1; then if ! dscl . read "/Groups/$GROUP" gid &> /dev/null; then
# Create the group
sudo dscl . create /Groups/"${GROUP}"
# Ensure the group has a GID
if [[ "$(sudo dscl . read /Groups/"$group" gid 2>&1)" == *"No such key"* ]]; then
MAX_ID_GROUP="$(dscl . -list /Groups gid | awk '{print $2}' | sort -ug | tail -1)" MAX_ID_GROUP="$(dscl . -list /Groups gid | awk '{print $2}' | sort -ug | tail -1)"
GROUP_ID="$((MAX_ID_GROUP+1))" PRIMARY_GROUP_ID="$((MAX_ID_GROUP+1))"
sudo dscl . create /Groups/"${GROUP}" gid "$GROUP_ID" dscl . create "/Groups/$GROUP"
fi # This also sets the PrimaryGroupID
sudo dscl . create "/Groups/$GROUP" gid "$PRIMARY_GROUP_ID"
sudo dscl . append "/Groups/$GROUP" GroupMembership "$USER"
else else
if command -v logg > /dev/null; then PRIMARY_GROUP_ID="$(dscl . read "/Groups/$GROUP" gid | awk '{print $2}')"
logg info "Group ${GROUP} already exists."
else
echo -e "\e[93mGroup ${GROUP} already exists\e[0m"
fi
fi fi
# Check if the user already exists # Ensure user exists
if ! sudo dscl . read /Users/"${GROUP}" >/dev/null 2>&1; then if ! dscl . read "/Users/$GROUP" UniqueID &> /dev/null; then
# Create the user
sudo dscl . create /Users/"${GROUP}"
# Ensure the user has a PrimaryGroupID
if [[ "$(sudo dscl . read /Users/"${GROUP}" PrimaryGroupID 2>&1)" == *"No such key"* ]]; then
sudo dscl . create /Users/"${GROUP}" PrimaryGroupID 20
fi
# Ensure the user has a UniqueID
if [[ "$(sudo dscl . read /Users/"${GROUP}" UniqueID 2>&1)" == *"No such key"* ]]; then
MAX_ID_USER="$(dscl . -list /Users UniqueID | sort -nr -k 2 | head -1 | grep -oE "[0-9]+$")" MAX_ID_USER="$(dscl . -list /Users UniqueID | sort -nr -k 2 | head -1 | grep -oE "[0-9]+$")"
USER_ID="$((MAX_ID_USER+1))" UNIQUE_ID="$((MAX_ID_USER+1))"
sudo dscl . create /Users/"${GROUP}" UniqueID "$USER_ID" sudo dscl . create "/Users/$GROUP"
sudo dscl . create "/Users/$GROUP" UniqueID "$UNIQUE_ID"
sudo dscl . create "/Users/$GROUP" PrimaryGroupID "$PRIMARY_GROUP_ID"
else
UNIQUE_ID="$(dscl . read "/Users/$GROUP" UniqueID | awk '{print $2}')"
fi fi
# Add the user to the group # Add the user to the group
sudo dseditgroup -o edit -t user -a "${GROUP}" "${GROUP}" sudo dseditgroup -o edit -t user -a "$GROUP" "$GROUP"
# Add the current user to the group # Add the current user to the group
sudo dseditgroup -o edit -t user -a "${USER}" "${GROUP}" sudo dseditgroup -o edit -t user -a "$USER" "$GROUP"
else # Add USER group to the group
if command -v logg > /dev/null; then sudo dseditgroup -o edit -t group -a "$USER" "$GROUP"
logg info "User ${GROUP} already exists"
else
echo -e "\e[93mUser ${GROUP} already exists\e[0m"
fi
fi
if command -v logg > /dev/null; then if command -v logg > /dev/null; then
logg info "Group and user ${GROUP} created successfully on macOS" logg info "Group and user ${GROUP} created successfully on macOS"
@ -127,6 +111,7 @@ elif [[ "$(uname)" == "Linux" ]]; then
else else
echo -e "\e[92mGroup and user ${GROUP} created successfully on Linux\e[0m" echo -e "\e[92mGroup and user ${GROUP} created successfully on Linux\e[0m"
fi fi
exit 0 exit 0
fi fi
@ -136,4 +121,5 @@ if command -v logg > /dev/null; then
else else
echo -e "\e[91mUnsupported operating system\e[0m" echo -e "\e[91mUnsupported operating system\e[0m"
fi fi
exit 1 exit 1