如何修改此脚本,以便可以通过提供参数来添加多个用户?
我至少知道我需要删除读取选项,但是我不想妥协用户名搜索,或者只有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用这样的构造遍历所有命令行参数:
只需调用每个参数的用户添加功能即可。
You can iterate through all command line arguments using construction like this:
Just call your user-adding function for each argument.