批量创建10个系统帐号,并设置密码(密码为随机数,要求字符和数字等混合)。
一‘ 简单思路
1 #!/bin/bash 2 for i in `seq 1 10` 3 do 4 if id user-$i > /dev/null;then 5 read -ep "user-$i用户已存在,是否删除(y/n)" ss 6 if [ $ss = y ];then 7 userdel -rf user-$i 8 elif [ $ss = n ];then 9 continue 10 else 11 echo "输入有误,user-$i未删除" 12 fi 13 else 14 useradd user-$i 15 if [ $? -eq 0 ];then 16 echo "user-$i 创建成功" 17 passwd="user`cat /dev/urandom | head -1 | md5sum | head -c 5`" 18 echo "user-$i:$passwd" chpasswd 19 echo "user-$i--$passwd" >> user.txt 20 else 21 echo "user-$i 创建失败" 22 fi 23 fi 24 done
二,思路高端
1 #!/bin/bash 2 echo "创建成功的用户" > /root/user_name 3 read -ep "请输入要创建的用户:" num 4 5 for i in `seq 1 $num` 6 do 7 pw=`cat /dev/urandom | head -1|md5sum |head -c 5` 8 id wg$i > /dev/null 2>&1 9 if [ $? -eq 0 ];then 10 while true 11 do 12 read -ep "wg$i用户已经存在,是否要删除此用户(y/n):" select 13 case $select in 14 y|Y) 15 userdel -rf wg$i > /dev/null 16 echo "wg$i删除成功" 17 break 18 ;; 19 n|N) 20 break 21 ;; 22 *) 23 echo "输入的字符有误,请重新输入" 24 esac 25 done 26 else 27 useradd wg$i > /dev/null 2>&1 28 if [ $? -eq 0 ];then 29 echo "wg$i用户创建成功" 30 echo "wg$pw" | passwd --stdin wg$i 31 echo "wg$i $pw" >> /root/user_name 32 else 33 echo "用户创建失败" 34 fi 35 fi 36 done 37 cat /root/user_name