Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.