Batch add and delete user scripts

Source: Internet
Author: User
#! /Bin/bash # This script is used to add or delete users in batches. The initial password is 123456. you need to change the password for the first login # Format :. /class_stu.sh parameter 1 parameter 2 parameter 3 # parameter 1: Start user name, which is a pure number and must be greater than 6 # Parameter 2: Number of users added [0 -...

#! /Bin/bash
#
# This script is used to add or delete users in batches. the initial user password is 123456. you need to change the password for the first login.
# Format:./class_stu.sh parameter 1 parameter 2 parameter 3
# Parameter 1: Start user name, which is a pure number and must be greater than 6 characters long
# Parameter 2: Number of users added [0-999]
# Parameter 3: Operation: 1-add, 2-delete
# Example:./class_stu.sh 2009034201 30 1 indicates adding 30 accounts starting from 2009034201
#./Class_stu.sh 2009034201 30 2 indicates deleting 30 accounts starting from 2009034201
# Class_stu.sh
#2011-8-21 LOPN
Export PATH =/bin:/sbin:/usr/bin:/usr/sbin
Function error ()
{
Echo "***** ERROR: the format is:./class_stu.sh par1 par2 par3"
Echo "***** EXAMPLE: class_stu.sh 2009034201 30 1"
Echo "***** NOTICE: par1 must is number and the length must rather 6"
Echo "***** NOTICE: par2 meanings the number of you want add [0-999]"
Echo "***** NOTICE: par3 respect your choice-add or del :"
Echo "***** 1: add memeber ***********"
Echo "***** 2: del memeber ***********"
Echo "***************** Other number is illegal **************** ****"
 
}
If [$ #! = 3]
Then
Error
Exit 1
Fi
Reciveinit1 = 'echo $1 | grep-e "^ [0-9] \ {6, \} $ "'
Reciveinit2 = 'echo $2 | grep-e "^ [0-9] \ {1, 3 \} $ "'
Operater = 'echo $3 | grep-e "^ [0-9] $ "'
If ["$1 "! = "$ Reciveinit1"] | ["$2 "! = "$ Reciveinit2"] | ["$3 "! = $3]
Then
Error
Exit 1
Fi

Initaccount = $1
Curaccount = $ initaccount
I = 0
Case $3 in
1)
Echo "*************************** add user ********* ***********************"
Echo-n "Are you ensure to add? [Y | Y/n | N]"
Read yes
If ["$ yes" = "y"-o "$ yes" = "Y"]
Then
While ["$ I "! = "$2"]
Do
Echo "*********** add user $ curaccount **********"
Ifexist = 'cat/etc/passwd | awk-F: "{print \ $1}" | grep $ curaccount'
If ["$ ifexist" = "$ curaccount"]
Then
Echo "************ the user $ curaccount already exists! *********"
Echo "******************* NEXT ***************** ****"
Else
Useradd $ curaccount
Echo "123456" | passwd -- stdin $ curaccount
Chage-d 0 $ curaccount
Echo "******* the user $ curaccount has been add! OK ***********"
Fi
I = $ ($ I + 1 ))
Curaccount = $ ($ curaccount + 1 ))
Done
Fi
;;
2)
Echo "************************** del user ********* ***********************"
Echo-n "Are you ensure to del user? [Y | Y/n | N]"
Read yes
If ["$ yes" = "y"-o "$ yes" = "Y"]
Then
While ["$ I "! = "$2"]
Do
Echo "******************** del user $ curaccount ************* ********"
Ifexist = 'cat/etc/passwd | awk-F: "{print \ $1}" | grep $ curaccount'
If ["$ ifexist" = "$ curaccount"]
Then
Userdel-r $ curaccount
Echo "****** the user $ curaccount has been deleted! OK ********"
Else
Echo "************* The USER $ curaccount does not exists! ***********"
Echo "*********************** NEXT! *******************************"
Fi
I = $ ($ I + 1 ))
Curaccount = $ ($ curaccount + 1 ))
Done
Fi
;;
*)
Echo "***************************** NOTICE ******* **********************"
Echo "please see the notice clear"
Error
;;
Esac
Echo "********************** the operator has been finished! ************************"
 
This article is from the lopnxrp 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.