Batch add users and add users

Source: Internet
Author: User

Batch add users and add users

A、creates a user file. Because there are more users, this script creates a User File user.txt #! /Bin/bash # create nine user names and their passwords for I in 'seq 9' do echo "user $ I passwd $ I"> user.txt doneb, after creating the user File, start adding. 1. Use the useradd and passwd commands. 2. There are a variety of loops used, such as for, while, and until, while reading rows. So what is the difference between using for or while? Take a look at the following instance #! /Bin/bash count = 0 for line in 'cat user.txt 'do echo $ line let count + = 1 done echo $ count result: user1 passwd1 user2 passwd2 user3 passwd3 user4 passwd4 user5 passwd5 user6 passwd6 user7 passwd7 user8 passwd8 user9 passwd9 18 #! /Bin/bash count = 0 while read line do echo $ line let count + = 1 done <user.txt echo $ count result: user1 passwd1 user2 passwd2 user3 passwd3 user4 passwd4 user5 passwd5 user6 passwd6 user7 passwd7 user8 passwd8 user9 passwd9 9 observed that while has a better row reading feature. When reading a file through the for loop, any blank characters will be used as the delimiter for reading the file, while using a line break. If other delimiters are used, both of them can be used. 3. truncate the row and separate the user name and password. #! /Bin/bash while read line do user = 'echo $ line | cut-d'-f1 'passwd = 'echo $ line | cut-d'-f2' echo $ user ": "$ passwd done <user.txt Note: Specify-d. The default is tab 4. After the screenshot is taken, the user and password are added #! /Bin/bash while read line do user = 'echo $ line | cut-d'-f1 'passwd = 'echo $ line | cut-d'-f2' # echo $ user ": "$ passwd useradd $ user passwd $ passwd done <user.txt result: Enter the new UNIX Password: re-enter the new UNIX Password: Sorry, passwords do not match passwd: authentication token operation error passwd: Password not changed enter new UNIX Password: re-enter new UNIX Password: Sorry, passwords do not match passwd: authentication token operation error passwd: enter a new UNIX Password without changing the password: re-enter the new UNIX Password: Sorry, passwords do not Match passwd: authentication token operation error passwd: password has not changed the original passwd requires the Administrator to manually enter the password! What should we do? Don't worry, use -- stdin while read line do user = 'echo $ line | cut-d'-f1 'passwd = 'echo $ line | cut-d'-f2' # echo $ user ": "$ passwd useradd $ user echo $ passwd | passwd -- stdin $ user done <user.txt result: passwd: The unrecognized option" -- stdin "is not supported by ubuntu. Continue to change :#! /Bin/bash while read line do user = 'echo $ line | cut-d'-f1 'passwd = 'echo $ line | cut-d'-f2' # echo $ user ": "$ passwd useradd $ user echo $ user": "$ passwd | chpasswd done <user.txt result: useradd: user" user1 "already has useradd: user" user2 "already has useradd: user "user3" already has useradd: User "user4" already has useradd: User "user5" already has useradd: User "user6" already has useradd: User "user7" already has useradd: user "user8" already has useradd: User "user9" already has this problem, but the password is still set. However, this is not correct. The effect should be that if the user exists, the password will not be reset. Otherwise, the password will be changed no matter whether the user exists or not, the password of the original user is reset. Write a batch Delete script #! /Bin/bash while read line do user = 'echo $ line | cut-d'-f1 'userdel-r $ user 2>/dev/null done <user.txt OK, re-Modify :#! /Bin/bash while read line do user = 'echo $ line | cut-d'-f1 'passwd = 'echo $ line | cut-d'-f2' # echo $ user ": "$ passwd useradd $ user 2>/dev/null & echo $ user": "$ passwd | chpasswd done <user.txt if the user already exists, but we cannot get any information when we add it. correct the information to make it human #! /Bin/bash while read line do user = 'echo $ line | cut-d'-f1 'passwd = 'echo $ line | cut-d'-f2' # echo $ user ": "$ passwd useradd $ user 2>/dev/null if [$? -Eq 0]; then echo $ user ":" $ passwd | chpasswd else echo "$ user exists, skip set passwd" fi done <user.txt c, end # final script #! /Bin/bash while read line do user = 'echo $ line | cut-d'-f1 'passwd = 'echo $ line | cut-d'-f2' # echo $ user ": "$ passwd useradd $ user 2>/dev/null if [$? -Eq 0]; then echo $ user ":" $ passwd | chpasswd else echo "$ user exists, skip set passwd" fi done <user.txt

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.