Linux systems write shell scripts to batch create and delete users

Source: Internet
Author: User
Tags stdin

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.