如何修改此脚本以便我可以通过提供参数来添加多个用户?

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

我知道至少我需要删除阅读选项,但我不想损害用户名搜索或只有 root/sudo 才能创建新用户

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 

您可以使用如下构造遍历所有命令行参数:

#!/bin/sh

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

只需为每个参数调用您的 user-adding 函数。