需求在服务器上创建180多个系统用户,并且uid、家目录、属组、shell、密码都是指定的,当时也是在网上搜了没有找到现成的脚本,自己写了一个,今天又完善了一下,给大家分享出来。
先新建用一个users.csv表格文件,文件格式如下:
A列用户名,B列组名,C列家目录,D列uid,E列shell类型,F列密码
脚本如下:
#!/bin/bash
input="users.csv"
if [ -f $input ];then
while IFS=, read -r name group homedir sum shell pass
do
if getent group $group &> /dev/null ;then
echo "----------------------------------------------"
if id $name &> /dev/null || id $sum &> /dev/null;then
echo "user: $name or $sum is exist."
continue 2
else
echo "create user: $name "
useradd -g $group -d $homedir -u $sum -s $shell $name &> /dev/null
if [ $shell == /sbin/nologin ]
then
echo "Without setting a password"
else
echo "$pass" | passwd --stdin $name &> /dev/null
fi
fi
else
echo "echo group $group is not exists."
fi
done < "$input"
else
echo "File $input not exists"
fi