使用文本文件中的用户清单批量创建Linux用户

脚本程序:

#!/bin/bash
read -p "Enter the users' password: " PASSWD
for USER in `cat users.txt`
do
id $USER &> /dev/null
if [ $? -eq 0 ]
then
echo "$USER already exists"
else
useradd $USER &> /dev/null
echo "$PASSWD" | passwd --stdin $USER &> /dev/null
if [ $? -eq 0 ]
then
echo "$USER, create success"
else
echo "$USER, create fail"
fi
fi
done

执行结果:

[hesper@husw ~]$ sudo ./shell.control.user.sh 
[sudo] password for hesper: 
Enter the users' password: zhujinzhi
hujunran, create success
zhujinzhi, create success
hushouwen, create success

结果检查:

[hesper@husw ~]$ tail -3 /etc/passwd
hujunran:x:1001:1001::/home/hujunran:/bin/bash
zhujinzhi:x:1002:1002::/home/zhujinzhi:/bin/bash
hushouwen:x:1003:1003::/home/hushouwen:/bin/bash

Leave a Reply

Your email address will not be published. Required fields are marked *