Home > Mobile >  How can I modify this script so that I can add multiple users by providing arguments?
How can I modify this script so that I can add multiple users by providing arguments?

Time:05-02

I know at the very least I need to remove the read options, but I don't want to compromise the username search or that only the root/sudo can create new users

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 

CodePudding user response:

You can iterate through all command line arguments using construction like this:

#!/bin/sh

while [ $# -gt 0 ]; do
    echo $1;
    shift
done

Just call your user-adding function for each argument.

  • Related