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

发布于 2025-01-25 14:04:00 字数 664 浏览 4 评论 0原文

我至少知道我需要删除读取选项,但是我不想妥协用户名搜索,或者只有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 

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2025-02-01 14:04:01

您可以使用这样的构造遍历所有命令行参数:

#!/bin/sh

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

只需调用每个参数的用户添加功能即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文