如何修改此脚本,以便可以通过在命令行中提供参数(用户名)来添加多个用户?
我知道至少我需要删除这些read
选项。我不想损害用户名搜索或只有 root/sudo 可以创建新用户。对 Linux 和 BASH 脚本非常陌生,请耐心等待。使用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