0

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
bossu
  • 1
  • 1
    Are you asking how to [Pass command line arguments to bash script](https://unix.stackexchange.com/questions/32290/pass-command-line-arguments-to-bash-script) ? – steeldriver May 02 '22 at 01:10
  • Actually yes and well this section has helped me! – bossu May 02 '22 at 01:26

0 Answers0