shell script combat - the amount of batch create / delete account

Preface: This batch script to create a user, but also bulk delete users, the specific operation for which the user is defined in a text file. And user-created this script, the first landing must change the password.

[root@localhost ~]# vim create_user.sh 

usergroup=""       #if your account need secondary group,add here
pwmech="openssl"   #"openssl" or "account" is needed.
homeperm="no" # if "yes" then I will modify home dir permission to 711
usepw="123456"               #this is all user init password
# 1.check the accountadd.txt file
action="${1}"        #"create" is useradd and "delete" is userdel
if [ ! -f accountadd.txt ];
        then
          echo "There is no accountadd.txt file,stop here"
        exit 1
fi
[ "${usergroup}" != "" ] && groupadd -r ${usergroup}
rm -f outputpw.txt
usernames=$(cat accountadd.txt)
for username in ${usernames}
do
        case ${action} in
          "create")
             [ "${usergroup}" != "" ] && usegrp=" -G ${usergroup} " || usegrp=""
             useradd ${usegrp} ${username}
             echo ${usepw} | passwd --stdin ${username}   #set password
             chage -d 0 ${username}
             [ "${homeperm}" == "yes" ] && chmod 711 /home/${username}
        echo "username=${username},password=${usepw}" >> outputpw.txt
             ;;
"delete")
               echo "deleting ${username}"
               userdel -r ${username}
               ;;
          *)
              echo "Usage:$0[create | delete]"
               ;;
        esac
done
[root@localhost ~]# cat accountadd.txt            #将需要创建的用户名写入该文件
lv1
lv2
lv3
lv4
#进行测试
[root@localhost ~]# sh create_user.sh create              #执行create选项,进行创建
更改用户 lv1 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 lv2 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 lv3 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 lv4 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@localhost ~]# tail -n 4 /etc/passwd                         #查看是否创建成功
lv1:x:1004:1005::/home/lv1:/bin/bash
lv2:x:1005:1006::/home/lv2:/bin/bash
lv3:x:1006:1007::/home/lv3:/bin/bash
lv4:x:1007:1008::/home/lv4:/bin/bash
[root@localhost ~]# sh create_user.sh delete                  #执行delete选项,进行删除
deleting lv1
deleting lv2
deleting lv3
deleting lv4

Guess you like

Origin blog.51cto.com/14154700/2435096