Skip to content

Instantly share code, notes, and snippets.

@miaoles
Created May 7, 2021 18:40
Show Gist options
  • Save miaoles/efc81d90f5a8e10404b3e2a56944fad6 to your computer and use it in GitHub Desktop.
Save miaoles/efc81d90f5a8e10404b3e2a56944fad6 to your computer and use it in GitHub Desktop.
CCM 2
#!/bin/bash
CONTINUE=false
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
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 $1) ]; 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.'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment