Skip to content

Instantly share code, notes, and snippets.

@dadambickford
Created November 27, 2019 23:43
Show Gist options
  • Save dadambickford/12b2c2b2b8fa04a1176024f582fef31d to your computer and use it in GitHub Desktop.
Save dadambickford/12b2c2b2b8fa04a1176024f582fef31d to your computer and use it in GitHub Desktop.
###### Initial user input
# ask users name and save input as NAME
read -p "Enter the name of the user being added, followed by [ENTER]: " NAME
# ask group name and save input as GROUP
read -p "Enter the name of the group being added, followed by [ENTER], (leave empty to skip): " GROUP
###### Helper functions
# gets a list of users and then searches for the first argument in that list
function check_for_user {
dscl . list /Users | grep -q $1
}
# gets a list of groups and then searches for the first argument in that list
function check_for_group {
dscl . list /Groups | grep -q $1
}
# logging indicator for fake commands
function fake_command {
printf "[FAKE] - $1\n"
}
###### Script Logic
# Validate user input for NAME
if [[ -z "$NAME" ]]; then
printf 'Name not entered, exiting script'
exit 1
else
# Check if user already exists
if check_for_user $NAME; then
printf "User ${NAME} found, skipping creation\n"
else
# Create user if no user found
fake_command "Create user ${NAME}" # Make sure to add flag that creates home directory
# ALTERNATIVELY: Ask the user for a password instead of generating one
# create temp password
TEMP_PASSWORD="${NAME}_12345"
# present admin running the script with the temp password
printf "Temporary password set to ${TEMP_PASSWORD} (please change asap)\n"
# Set password for the user
fake_command "Set user ${NAME} password to ${TEMP_PASSWORD}"
fi
fi
# Validate user input for GROUP
if [[ -z "$GROUP" ]]; then
# If no group entered, exit the script as we are done
printf 'Group not entered, skipping add to group\n'
exit 0
else
# Check if group already exists
if check_for_group $GROUP; then
# Add to group if it exists already
printf "Group ${GROUP} found, skipping creation\n"
fake_command "Add user ${NAME} to group ${GROUP}"
else
fake_command "Create group ${GROUP}"
fake_command "Add user ${NAME} to group ${GROUP}"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment