批量新建用户uer1~user20,如果用户存在则提示用户已经存在,不添加;如果不存在则添加用户,并给该用户指定一个10位的随机密码,输出到某个文件中。
#!/bin/bash # log=useradd.log echo "`date +%F-%T `try to add users.">> $log for i in `seq 1 20`;do password=`mkpasswd -l 10` if id user$i &>/dev/null ;then echo "user$i is exist!">>$log else useradd user$i && echo $password |passwd user$i --stdin &>/dev/null echo "Add user$i succeed ! username:user$i,password:$password">>$log fi done
执行两次后的结果
[root@localhost ~]# cat useradd.log 2015-03-26-18:19:23try to add users. Add user1 succeed ! username:user1,password:fmQ2w~3Ejo Add user2 succeed ! username:user2,password:vj6uoVt8V$ Add user3 succeed ! username:user3,password:xr8!x3jCYu Add user4 succeed ! username:user4,password:qh7Bs?p4Vx Add user5 succeed ! username:user5,password:l9e"2gdgHS Add user6 succeed ! username:user6,password:4yn9HUn>jk Add user7 succeed ! username:user7,password:Z[vug05Pda Add user8 succeed ! username:user8,password:01Z?czgAgi Add user9 succeed ! username:user9,password:s|tCXk79fm Add user10 succeed ! username:user10,password:Baq-v7gd8L Add user11 succeed ! username:user11,password:6xv@zrT5zG Add user12 succeed ! username:user12,password:v13RGjdcp" Add user13 succeed ! username:user13,password:g~meVt43xF Add user14 succeed ! username:user14,password:Z^l0rd5Qml Add user15 succeed ! username:user15,password:9ytXmmG\z6 Add user16 succeed ! username:user16,password:b23ecuI:dT Add user17 succeed ! username:user17,password:l7Xkkb9!yP Add user18 succeed ! username:user18,password:v26uwRQ"bq Add user19 succeed ! username:user19,password:b[3OA3bmrj Add user20 succeed ! username:user20,password:vwXIr68;al 2015-03-26-18:20:22try to add users. user1 is exist! user2 is exist! user3 is exist! user4 is exist! user5 is exist! user6 is exist! user7 is exist! user8 is exist! user9 is exist! user10 is exist! user11 is exist! user12 is exist! user13 is exist! user14 is exist! user15 is exist! user16 is exist! user17 is exist! user18 is exist! user19 is exist! user20 is exist!
转载于:https://blog.51cto.com/onelinux/1624570