Shell programming Exercises-Bulk Create delete users and groups

Source: Internet
Author: User
Tags stdin

1. Requirements

Reed is a system administrator who needs to add a teacher and several student users to a server in a classroom, manually adding too much trouble, and ask you to write a bash script for him userctr.sh implement bulk add and delete users. Teacher user name, student user name and student number use parameters to control.

userctr.sh script execution time consists of four parameters:

bash userctr.sh 操作(add或者del)教师名 学生名前缀 学生数量

After the script executes successfully, 1 teacher users and several student users are created, meeting the following criteria:

1) Number of students parameter, the parameter range is 1~10, if more than 10 or is not a positive integer, then error printing parameter error
2) Student name prefix is a string, only allow to contain lowercase letters, otherwise error print parameter error, prefix followed by the number sequence
3) Each user sets a random 6-digit password, after the Add command executes and the user name and corresponding password output
4) If a user name already exists, you do not need to create the user by default

Examples of executing scripts:

#添加一位叫reeeed教师,3位叫deeeer的学生[[email protected] ~]# ./userctr.sh add reeeed deeeer 3User [reeeed] create successfully.reeeed:2bf168User [deeeer1] create successfully.deeeer1:39adcdUser [deeeer2] create successfully.deeeer2:a12201User [deeeer3] create successfully.deeeer3:4088d2#参数不正确时提示[[email protected] ~]# ./userctr.sh add 123 321 11Parameter error!#输入不规范时的提示[[email protected] ~]# ./userctr.sh add 123 321 --Usage:        ./userctr.sh [add/del] [Teacher‘sName] [Student‘sName] [The number of Students]        ex.        ./userctr.sh add teacher stu 3--        add:useradd username        del:userdel username        [Teacher‘sName]:teacher‘s name        [Student‘sName]:student‘s name,just only the beginning of a lowercase letter         [The number of Students],only [1-10]

2. Difficulties and knowledge points
Difficulties:

1. Need to determine whether the input parameters are correct and specification
2. Nesting multiple conditional judgment statements
3. Randomly generated passwords

Knowledge Points:

If/for syntax in 1.SHELL
2.SHELL parameter usage
3. User-Created

Specific implementation:

[[email protected] ~]# cat userctr.sh #!/bin/bash#func:add or del user#author:reed. /etc/profileuserctr=$1teachername=$2studentname=$3studentnumber=$4usage () {cat<<eof--usage: $ [Add/del] [        Teacher ' sName] [Student ' sName] [the number of Students] ex.        $ add teacher Stu 3--add:useradd username del:userdel username [teacher ' sname]:teacher ' s name [Student ' sname]:student ' s name,just only the beginning of a lowercase letter [the number of students],only [1-10  ]eof}if [$#-eq 4];then if [$ = = ' Add '];then #echo "Add" if [["$" =~ ^[a-z]+$                         && "$ $" =~ ^[a-z]+$ && "$4"-ge 1 && $4-le #create User $                                ID $ >>/dev/null 2>&1 if [$?-ne 0];then               Useradd teacherpasswd=$ (date|md5sum|cut-c 1-6)                 echo $TeacherPasswd |passwd--stdin >>/dev/null 2>&1 echo "                                User [$] Create successfully. "                        echo "$: $TeacherPasswd" Else echo "the user [$] is exist." Fi for ((num=1;num<=$4;num++));d o ID ${3}${num} >>/dev/null 2>&1 if [$?-ne 0];then u                                        Seradd ${3}${num} studentpasswd=$ (echo $RANDOM |md5sum|cut-c 1-6)                                        echo $StudentPasswd |passwd--stdin ${3}${num} >>/dev/null 2>&1                                        echo "User [${3}${num}] create successfully."                                      echo "${3}${num}: $StudentPasswd" Else  echo "The user [${3}${num}] is exist."                        Fi done Else echo "Parameter error!"        Exit 10000 fi elif [$ = = "Del"];then echo "please delete user manually." else echo "Parameter error!                Please input [add] or [del]. " Exit 10086 Fielse Usagefi

Note: the "del" User deletion principle is the same as creating a user, lazy write.

Shell programming Exercises-Bulk Create delete users and groups

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.