shell习题-批量创建用户并设置密码

1.要求:

添加user_00 – user_09 10个用户,并且给他们设置一个随机密码,
密码要求10位包含大小写字母以及数字,
注意需要把每个用户的密码记录到一个日志文件里。

2.脚本答案:

[root@liang ~]# cat /scritp/2018-06-14/usercreate,sh 

#!/bin/bash
for i in `seq -w 00 09`
do
        password=`/usr/bin/echo $RANDOM |md5sum |cut -c 1-10` 
        echo "$password" >>/test/password.log
        useradd user_${i}
        if [ $? -eq 0 ];then
                echo "$password"|passwd --stdin user_${i}
        fi
done


3.执行脚本:

[root@liang 2018-06-14]# bash usercreate.sh 
Changing password for user user_00.
passwd: all authentication tokens updated successfully.
Changing password for user user_01.
passwd: all authentication tokens updated successfully.
Changing password for user user_02.
passwd: all authentication tokens updated successfully.
Changing password for user user_03.
passwd: all authentication tokens updated successfully.
Changing password for user user_04.
passwd: all authentication tokens updated successfully.
Changing password for user user_05.
passwd: all authentication tokens updated successfully.
Changing password for user user_06.
passwd: all authentication tokens updated successfully.
Changing password for user user_07.
passwd: all authentication tokens updated successfully.
Changing password for user user_08.
passwd: all authentication tokens updated successfully.
Changing password for user user_09.
passwd: all authentication tokens updated successfully.

4.密码日志

[root@liang 2018-06-14]# cat -n /test/password.log 
     1  8f173d44c5
     2  3191176109
     3  eb69246aac
     4  8e779f3fcd
     5  64ce0eea97
     6  37dab49727
     7  0e0b3ba522
     8  8845012401
     9  4f80f29512
    10  4f244042b3

5.删除用户:

[root@liang 2018-06-14]# tail -10 /etc/passwd|awk -F ":" '{print "userdel -r "$1}'|bash

你可能感兴趣的:(shell脚本练习)