#!/bin/bash
echo "创建成功的用户" > /root/user_name
read -ep "请输入要创建的用户:" num
for i in `seq 1 $num`
do
pw=`cat /dev/urandom | head -1|md5sum |head -c 5` #生成随机5位密码
id user-$i > /dev/null 2>&1 #id 用户名 查看是否存在用户
if [ $? -eq 0 ];then
while true
do
read -ep "user-$i用户已经存在,是否要删除此用户(y/n):" choice
case $choice in
y|Y|yes)
userdel -rf user-$i > /dev/null
echo "user-$i删除成功"
break
;;
n|N|no)
break
;;
*)
echo "输入的字符有误,请重新输入"
esac
done
else
useradd user-$i > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "user-$i用户创建成功"
echo "user-$pw" | passwd --stdin user-$i #给用户添加
echo "user-$i $pw" >> /root/user_name #把账号密码添加到文件user_name中
else
echo "用户创建失败"
fi
fi
done
cat /root/user_name