Create multiple users by using the shell operation file

Source: Internet
Author: User
Functional requirements: 1. create a new user. generally, create a sub-directory named "/home". 2. add a new user record to the/etc/passwd file. 3. add new user records to the/etc/shadow file. Code implementation: the while, chown, chgrp, and other statements are used. #! /Bin...

Functional requirements: 1. create a new user, which is usually a subdirectory named by user name in/home.
2. add new user records in the/etc/passwd file.
3. add new user records to the/etc/shadow file.
Code implementation:
Use the while, chown, chgrp, and other statements.
#! /Bin/bash // define the script execution environment
# Filename: create a file // define a file name
I = 1 // define the variable
Awk 'In in {FS = ":" ;}{ print $3 ;} '/etc/passwd> pa // indicates to save the bottom three columns of the passwd file with the colon separator to the pa file, "BEGIN" indicates processing starts from the first character of each line in the/etc/passwd file. the code in this line extracts the third line from the file/etc/passwd and saves it to the file pa.
X = 'tail-200' // save the first line of the file pa to variable x, that is, variable x stores the largest user ID currently. here, "· tail-l Pa'" is quoted in reverse quotation marks.
X = $ ($ x + 1) // add the x value of the variable to 1 as the id of the next group of new users.
While [$ I-le 50] // The loop structure is less than 50 and more than 50 ends the loop body.
The structure in the do // loop statement.
Mkdir-p/home/stud $ {I} // use the mkdir command to create the user's home directory,
Chown-R $ stud $ {I}/home/$ stud $ {I} // use chown to specify the main directory,
Chgrp-R user1/home/$ stud $ {I} // use chgrp to specify the main directory
Echo "stud $ {I}: x :$ {x}: 100:/home/stud $ {I }: /bin/bash ">/etc/passwd // Append the string to the end of/etc/passwd
Echo "stud $ {I }:: 14098: 0: 9999: 7: ::::" >>/ etc/shadow // Append the string to the end of the file
I = $ ($ I + 1) // add 1 to the variable
X = $ ($ x + 1) // add the variable x value to 1
Done // indicates the end of the loop body
 
This article is from the "Happy first" blog

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.