Batch create user account scripts in linux and linux User Account scripts

Source: Internet
Author: User

Batch create user account scripts in linux and linux User Account scripts

In the book "linux private house dish" of niniao brother, I found that I forgot what I learned. to get familiar with some commands, let's take a look at the example of creating accounts in batches in chapter 14 by using shell scripts. The following code is annotated by laruence and slightly changed to conform to the school's compiling.

1. Create accounts in batches and save the information to files

The password generation method is not provided in the book. The password is the same as the account, and the user is forced to change the password after logon. This solution is safer.

1 #! /Bin/bash 2 #3 # this program use to add account for your linux 4 # referece: vbird's private dish. 5 #6 # History: 7 8 9 # Save the account password file 10 accountfile = "user. passwd "11 12 read-p" input grade: for example (2015): "username_grade13 read-p" input class number: for example (1122 ): "username_class14 read-p" number of digits entered: for example, (2): "nu_nu15 read-p" Enter the start number, for example, (1): "nu_start16 read-p" Enter the number of accounts: for example, (10): "nu_amount17 18 if [" $ username_grade "=" "-o" userna Me_class "=" "]; then 19 echo" enter grade class information! "; 20 exit 121 fi22 23 # determine whether the number is valid 24 testing0 =$ (echo $ nu_nu | grep '^ 0-9 ') 25 testing1 = $ (echo $ nu_start | grep '^ 0-9') 26 testing2 = $ (echo $ nu_amount | grep '^ 0-9') 27 28 if ["$ testing0 "! = ""-O "$ testing1 "! = ""-O "$ testing2 "! = ""]; Then29 echo "incorrect number! "; 30 exit 131 fi 32 33 # Add date 34 [-f "$ accountfile"] & mv $ accountfile "$ accountfile" $ (date + % Y % m % d % s) 35 36 nu_end = $ ($ nu_start + $ nu_amount-1) 37 38 for (I = $ nu_start; I <= $ nu_end; I ++ )) 39 do 40 # $ {I} statistic number I length 41 nu_len =$ {# I} 42 if [$ nu_nu-lt $ nu_len]; then43 echo "the input is unreasonable, the number is insufficient or the number of accounts is too large! "44 echo" please check it out "45 exit 146 fi47 48 # number of digits that need to be zero before the number 49 nu_diff = $ ($ nu_nu-$ nu_len )) 50 51 nu_nn = "" 52 if ["$ nu_diff "! = "0"]; then53 nu_nn = 000000000054 nu_nn =$ {nu_nn: 1: $ nu_diff} 55 fi56 57 # grade the above information, class, the serial number is combined to form an account 58 account =$ {username_grade }$ {username_class }$ {nu_nn }$ {I} 59 password = "$ account" 60 61 # account password writes to file 62 echo account: "$ account": password: "$ password" | tee-a "$ accountfile" 63 done64 65 # new account password 66 usernames = $ (cat "$ accountfile" | cut-d': '-f2) 67 68 for u in $ usernames69 do70 useradd $ u71 echo $ u | passwd -- stdin $ u72 # Force Logon repair Change Password 73 chage-d 0 $ u74 75 done76 77 echo "OK! Create $ nu_amount accounts ."

If the students in the same class need to change 7 rows to useradd-g username_class $ u in the same group

2. Batch Delete the created accounts from the files saved just now

What should I do if I fail to create a hand defect? delete accounts in batches

1 #! /Bin/bash2 # to del the user from file3 4 usernames = $ (cat user. passwd | cut-d': '-f2) 5 for username in $ usernames6 do7 echo "delete user $ username" 8 userdel-r $ username9 done

Attach the original address http://linux.vbird.org/linux_basic/0410accountmanager/account2.sh provided on laruence books

I didn't duplicate the wheel. I was just practicing.

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.