Linux shell add users in batches

Require:

  1. Create a script batchuser.sh to create local users for the system
  2. and the usernames come from a file containing a list of usernames
  3. User login shell is /bin/false
  4. The default login password is: 123456

step:

user.txt

stu11
stu22
stu33

vim batchuser.sh

#!/bin/bash
# 批量添加用户
# 要求提供一个包含用户列表的参数
if [ $# -gt 0 ]
then
        if [ ! -e $1 ]
        then
                echo "$1 does not exist"
        else
                for user in `cat $1`
                do
                        useradd $user -s /bin/false && echo "123456" | passwd --stdin $user
                done
        fi
else
        echo "Usage: batchuser [user_list]"
fi

result:

[root@server1 ~]# bash batchusers.sh users.txt
更改用户 stu11 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 stu22 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 stu33 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@server1 ~]# grep stu /etc/passwd | tail -n 3
stu11:x:1021:1024::/home/stu11:/bin/false
stu22:x:1022:1025::/home/stu22:/bin/false
stu33:x:1023:1026::/home/stu33:/bin/false

Guess you like

Origin blog.csdn.net/dgwxligg/article/details/129156340