1.需求

reed是系统管理员,需要为一个教室中的服务器添加一个老师和若干学生用户,手动添加太麻烦了,请你为他编写一个bash脚本 userctr.sh 实现批量添加和删除用户。老师用户名,学生用户名和学生数量使用参数进行控制。

userctr.sh 脚本执行时候包括四个参数:

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

脚本成功执行后将创建1个教师用户和若干个学生用户,满足下列条件:

1)学生数量参数,参数范围为1~10,若超过10或不为正整数,则报错打印 parameter error
2)学生名前缀为字符串,只允许包含小写字母,否则报错打印 parameter error,前缀后面跟数字序列
3)每个用户设置一个随机6位数密码,在添加命令执行后并将用户名和对应的密码输出
4)如果某个用户名已经存在,则默认不需要创建该用户

执行脚本的范例:

#添加一位叫reeeed教师,3位叫deeeer的学生
[root@reedoracle ~]# ./userctr.sh add reeeed deeeer 3
User [reeeed] create successfully.
reeeed:2bf168
User [deeeer1] create successfully.
deeeer1:39adcd
User [deeeer2] create successfully.
deeeer2:a12201
User [deeeer3] create successfully.
deeeer3:4088d2
#参数不正确时提示
[root@reedoracle ~]# ./userctr.sh add 123 321 11
Parameter error!
#输入不规范时的提示
[root@reedoracle ~]# ./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.难点和知识点
难点:

1.需要判断输入的参数是否正确和规范
2.嵌套多个条件判断语句
3.随机生成密码

知识点:

1.SHELL中的if/for语法
2.SHELL参数使用
3.用户创建

具体实现:

[root@reedoracle ~]# cat userctr.sh 
#!/bin/bash

#func:add or del user
#author:reed

. /etc/profile

UserCtr=$1
TeacherName=$2
StudentName=$3
StudentNumber=$4

Usage(){
cat<<EOF
--Usage:
        $0 [add/del] [Teacher'sName] [Student'sName] [The number of Students]
        ex.
        $0 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 [ $1 == "add" ];then
                #echo "add"
                if [[ "$2" =~ ^[a-z]+$  &&  "$3" =~ ^[a-z]+$  && "$4" -ge 1 && "$4" -le 10 ]];then
                        #create user $2
                        id $2 >>/dev/null 2>&1
                        if [ $? -ne 0 ];then
                                useradd $2
                                TeacherPasswd=$(date|md5sum|cut -c 1-6)
                                echo $TeacherPasswd|passwd --stdin $2 >>/dev/null 2>&1
                                echo "User [$2] create successfully."
                                echo "$2:$TeacherPasswd"
                        else
                                echo "The user [$2] is exist."
                        fi
                        for ((num=1;num<=$4;num++));do
                                id ${3}${num} >>/dev/null 2>&1
                                if [ $? -ne 0 ];then
                                        useradd ${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 [ $1 == "del" ];then
                echo "Please delete user manually."
        else
                echo "Parameter error!Please input [add] or [del]."
                exit 10086
        fi
else
        Usage
fi

注:“del”用户删除的原理与创建用户一样,懒的写了。