First, write shell scripts to add users in bulk
Implementation method: To determine whether the user exists, there is a return error prompt, while determining whether the user file exists, does not exist then exit
1. Create Add User Script
[Email protected] ~]# vim useradd.sh
#!/bin/bash
If [$#-eq 0];then
echo "You didn't enter any files!"
Exit 1
Fi
if [!-F $];then
echo "Wrong input! "
Exit 2
Fi
For user in ' cat $ '
Do
ID $user &>/dev/null
If [$?-eq 0];then
echo "User $user already exists! "
Else
Useradd-s/sbin/nologin $user
echo "Password" | passwd--stdin $user
echo "User $user created successfully. "
Fi
Done
2. Check the script and add executable permissions
[Email protected] ~]# sh-n useradd.sh
[Email protected] ~]# chmod +x useradd.sh
3. Add Users
[Email protected] ~]#./useradd.sh
You did not enter any files!
[Email protected] ~]#/useradd.sh user.txt
Change the password for the user user1.
PASSWD: All the authentication tokens have been successfully updated.
User User1 created successfully.
Change the password for the user user2.
PASSWD: All the authentication tokens have been successfully updated.
User User2 created successfully.
Change the password for the user User3.
PASSWD: All the authentication tokens have been successfully updated.
User User3 created successfully.
Change the password for the user user4.
PASSWD: All the authentication tokens have been successfully updated.
User User4 created successfully.
Change the password for the user user5.
PASSWD: All the authentication tokens have been successfully updated.
User User5 created successfully.
Second, delete the user
1. Write the Delete script
[Email protected] ~]# vim userdel.sh
#!/bin/bash
#userdel
If [$#-eq 0];then
echo "You didn't enter the file! "
exit 1
Fi
if [!] -F "$"];then
echo "wrong input! "
exit 2
Fi
for user in ' cat $ '
do
ID $user &>/dev/null
if [$-eq 0];then
Userdel-r $user &>/dev/null
echo "Delete $user success!
Else
echo "user does not exist! "
fi
Done
[Email protected] ~]# sh-n userdel.sh
[Email protected] ~]# chmod a+x userdel.sh
2. Delete users
[Email protected] ~]#/userdel.sh 111
Wrong input!
[Email protected] ~]#/userdel.sh user.txt
Delete User1 success!
Delete User2 success!
Delete User3 success!
Delete User4 Success!
Delete User5 success!
[Email protected] ~]#/userdel.sh user.txt
User does not exist!
User does not exist!
User does not exist!
User does not exist!
User does not exist!
While read user
Do
ID $user &>/dev/null
If [$?-eq 0];then
echo "User $user already exists! "
Else
Useradd-s/sbin/nologin $user
echo "Password" | passwd--stdin $user
echo "User $user created successfully. "
Fi
Done < $
Linux systems write shell scripts to batch create and delete users