一、项目需求
批量创建10个用户stu01-stu10,并且设置随机8位密码,要求:不能用shell循环(例如:for,while等),只能用linux命令及管道实现。
二、实现方法(一)
1) 创建账号并设置随机密码
[[email protected] ~]# for n in seq 10
;do useradd user$n;echo "user$necho user$n|md5sum|cut -c4-8
"|passwd --stdin user$n;done
2)打印密码并导入文件
[[email protected] ~]# for n in seq 10
;do echo "user$necho user$n|md5sum|cut -c4-8
";done>>/tmp/Tim1
3)打印用户并导入文件
[[email protected] ~]# for n in seq 10
;do echo user$n;done>>/Tim2
4)合并用户密码文件
[[email protected] ~]# paste -d ":" /Tim2 /tmp/Tim1 >>/tmp/newpasswd
[[email protected] ~]# cat /tmp/newpasswd
user1:user193167
user2:user22d1d7
user3:user3c489e
user4:user44890e
user5:user5ca814
user6:user6957ba
user7:user72c5fb
user8:user83428e
user9:user949703
user10:user100dc3e
三、实现方法(二)
1)创建用户并设置密码
[[email protected] ~]# for n in seq 11 16
;do useradd user$n;echo "user$n:user$necho user$n|md5sum|cut -c4-8
"|chpasswd;done
[[email protected] ~]# tail -6 /etc/passwd
user11:x:517:517::/home/user11:/bin/bash
user12:x:518:518::/home/user12:/bin/bash
user13:x:519:519::/home/user13:/bin/bash
user14:x:520:520::/home/user14:/bin/bash
user15:x:521:521::/home/user15:/bin/bash
user16:x:522:522::/home/user16:/bin/bash
2)导出用户密码到文件
[[email protected] ~]# for n in seq 11 16
;do echo "user$n:user$necho user$n|md5sum|cut -c4-8
";done>>/tmp/newpasswd1
[[email protected] ~]# cat /tmp/newpasswd1
user11:user11b755c
user12:user121deb5
user13:user13f627d
user14:user1472383
user15:user15c5412
user16:user16eb606
原文地址:http://blog.51cto.com/11956937/2062036