Skip to content

Instantly share code, notes, and snippets.

@jadedgnome
Created April 4, 2015 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jadedgnome/7d340c547f81247fa29c to your computer and use it in GitHub Desktop.
Save jadedgnome/7d340c547f81247fa29c to your computer and use it in GitHub Desktop.
shell script to create a user account on linux
#!/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