Below is a quick script to create a user account and password.
If no username is typed the script will exit. If the input for for the username is “Exit” or “exit” the script will exit.
When finished typing the username and password the script will show the output of the newly created user in /etc/passwd and loop back to the beginning and will ask to create another user account.
#!/bin/bash
# Script for creating user accounts
# Function Declarations
err_function() {
echo An error has occurred!
echo Exiting script immediately...
exit
}
# While loop to repeat user account creation process
while true
do
# Create user account or exit the script
echo -e "Please type new account name or type Exit to leave: \c"
# Read user Input
read -r Username
# If account is not provided, leave script
if [ -z "$Username" ]
then
echo
echo An account name was not provided
echo Leaving script...
exit
fi
# Type "Exit" or "exit" to leave script
case "$Username" in
Exit)
echo Leaving script...
exit
;;
exit)
echo Leaving script...
exit
;;
*)
echo
echo Account for "$Username" is being created...
;;
esac
# Create user with home directory from user Input
# Double pipes with the function are used to cleanly exit the script if an error occurs
useradd -m -d "$Username" || err_function
# Create password for account
echo Password "$Username" is being set...
passwd "$Username"
# Display account record
User_Record=$(grep "$Username" /etc/passwd)
echo "$User_Record"
done
Got a project that needs expert IT support?
From Linux and Microsoft Server to VMware, networking, and more, our team at CR Tech is here to help.
Get personalized support today and ensure your systems are running at peak performance or make sure that your project turns out to be a successful one!
CONTACT US NOW