#!/bin/bash
for i in `seq 1 10`
do
if id user-$i > /dev/null;then
read -eq "user-$i用户已存在,是否删除(y/n)" ss
if [ $ss = y ];then
userdel -rf user-$i
elif [ $ss = n ];then
continue
else
echo "输入有误,user-$i用户未删除"
fi
else
useradd user-$i
if [ $? -e 0 ];then
echo "user-$i 用户创建成功"
passwd="user`cat /dev/urandom | head -1 | md5sum | head -c 5`"
echo "user-$i:$passwd" chpasswd
echo "user-$i--$passwd" >> user.txt
else
echo "user-$i 用户创建失败"
fi
fi
done
这是一种方法比较简单理解,还有另一种
#!/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`
id wg$i > /dev/null 2>&1
if [ $? -eq 0 ];then
while true
do
read -ep "wg$i用户已经存在,是否要删除此用户(y/n):" select
case $select in
y|Y)
userdel -rf wg$i > /dev/null
echo "wg$i删除成功"
break
;;
n|N)
break
;;
*)
echo "输入的字符有误,请重新输入"
esac
done
else
useradd wg$i > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "wg$i用户创建成功"
echo "wg$pw" | passwd --stdin wg$i
echo "wg$i $pw" >> /root/user_name
else
echo "用户创建失败"
fi
fi
done
cat /root/user_name