Skip to content

Instantly share code, notes, and snippets.

@miaoles
Created May 7, 2021 19:59
Show Gist options
  • Save miaoles/6da22b8303b3299624e95db67ad4725a to your computer and use it in GitHub Desktop.
Save miaoles/6da22b8303b3299624e95db67ad4725a to your computer and use it in GitHub Desktop.
#!/bin/bash
CONTINUE=false
USERNAME=null
GROUPNAME=null
check_username () {
if [ id -u "$USERNAME" >/dev/null 2>&1 ]; then
echo 'This user already exists.'
CONTINUE=false
else
echo 'This username is available. The user will be created.'
sudo useradd -m $USERNAME
CONTINUE=true
fi
}
check_groupname () {
if [ $(getent group $GROUPNAME) ]; then
echo 'This group already exists. The user will be added to it.'
else
echo 'This group does not exist. The group will created and the user will be added to it.'
sudo groupadd $GROUPNAME
fi
sudo usermod -a -G $GROUPNAME $USERNAME
echo "User $USERNAME was created and added to group $GROUPNAME."
}
echo 'Enter the username for a new user:'
read USERNAME
check_username
if [ $CONTINUE = true ]; then
echo 'Enter the groupname to assign the new user to:'
read GROUPNAME
check_groupname
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment