Created
May 7, 2021 18:40
-
-
Save miaoles/efc81d90f5a8e10404b3e2a56944fad6 to your computer and use it in GitHub Desktop.
CCM 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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