How can I modify this script so that I can add multiple users by providing arguments (usernames) in the command line instead?
I know at the very least I need to remove the read
options. I don't want to compromise the username search or that only the root/sudo can create new users. Very new to Linux and BASH scripting so bear with me. Using CentOS8.
#!/bin/bash
if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
sleep 1
read -s -p "Enter password : " password
sleep 2
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
sudo useradd -m -p "$pass" "$username"
[ $? -eq 0 ] &&
echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Only root may add a user to the system."
exit 2
fi