Created
April 4, 2015 13:32
-
-
Save jadedgnome/7d340c547f81247fa29c to your computer and use it in GitHub Desktop.
shell script to create a user account on linux
This file contains 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 | |
clear | |
trap "" SIGHUP SIGINT SIGTERM SIGTSTP | |
#get username, check if its taken, and if its proper length | |
while true | |
do | |
echo -n "Create username: " | |
read username | |
if [ ${#username} -gt 0 ] | |
then | |
if [ $(cat /etc/passwd | grep $username) ] | |
then | |
echo "Username already exists. Please choose another one." | |
elif [ ${#username} -gt 2 ] && [ ${#username} -lt 10 ] | |
then | |
break | |
else | |
echo "Username must be between 2 to 10 characters." | |
fi | |
else | |
echo "Username can not be blank." | |
fi | |
done | |
#get passwords and check if they're the same | |
while true | |
do | |
echo -n "Password: " | |
read password | |
echo -n "Confirm password: " | |
read password_confirm | |
if [ ${#password} -gt 0 ] || [ ${#password_confirm} -gt 0 ] | |
then | |
if [ $password == $password_confirm ] | |
then | |
break | |
else | |
echo "Passwords do not match." | |
fi | |
else | |
echo "Password can not be blank." | |
fi | |
done | |
#add user | |
useradd $username -m -s /bin/bash -G users | |
#give user password | |
echo $username:$password | chpasswd | |
echo "Account created! Please login to your new account." | |
kill -HUP $PPID |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment