#!/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
}
命令 chmod u+x demo.sh
./demo.sh add zhanglaoshi student 5
if [ KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲ -eq 4 ];then …#所有参数总和 add zhanglaoshi student 5**
if [ $1 == “add” ];then //第一个参数
#echo “add”
if [[ "
2
"
=
[
a
−
z
]
+
2" =~ ^[a-z]+
2"= [a−z]+ && "
3
"
=
[
a
−
z
]
+
3" =~ ^[a-z]+
3"= [a−z]+ //以小写字母开始 && “$4” -ge 1 && “$4” -le 10 //学生个数范围]];then
#create user $2
id $2 >>/dev/null 2>&1
//id用户的详细信息,检查用户是否存在 ,2>&1 ,把输入输出的信息都定向到黑洞中,否则 2>>/dev/null
if [
?
−
n
e
0
]
;
t
h
e
n
∗
∗
/
/
? -ne 0 ];then **//
?−ne0];then∗∗//? 上一条命令的返回值**
useradd
2
∗
∗
/
/
教
师
用
户
∗
∗
T
e
a
c
h
e
r
P
a
s
s
w
d
=
2 **//教师用户** TeacherPasswd=
2∗∗//教师用户∗∗TeacherPasswd=(date|md5sum|cut -c 1-6) //教师用户密码 date 日期,md5sum 对时间加密,加密方式,数字或字母,cut -c 截取一到六位,这样密码不会重复
echo $TeacherPasswd|passwd --stdin $2 >>/dev/null 2>&1 //将程序中的变量值作为键盘的标准输入
echo “User [$2] create successfully.”
echo “
2
:
2:
2:TeacherPasswd”
else
echo “The user [$2] is exist.”
Fi
//创建普通学生用户
for ((num=1;num<=$4;num++));do
id
3
{3}
3{num} >>/dev/null 2>&1
if [ $? -ne 0 ];then
useradd
3
{3}
3{num}
StudentPasswd=$(echo
R
A
N
D
O
M
∣
m
d
5
s
u
m
∣
c
u
t
−
c
1
−
6
)
∗
∗
/
/
RANDOM|md5sum|cut -c 1-6) **//
RANDOM∣md5sum∣cut−c1−6)∗∗//RANDOM随机数**
echo $StudentPasswd|passwd --stdin
3
{3}
3{num} >>/dev/null 2>&1
echo “User [
3
{3}
3{num}] create successfully.”
echo “
3
{3}
3{num}:
S
t
u
d
e
n
t
P
a
s
s
w
d
"
e
l
s
e
e
c
h
o
"
T
h
e
u
s
e
r
[
StudentPasswd" else echo "The user [
StudentPasswd"elseecho"Theuser[{3}${num}] is exist.”
fi
done
else
echo “Parameter error!”
exit 10000
fi
elif [ $1 == “del” ];then
id $2 >>/dev/null 2>&1
if [ $? -ne 0 ];then
userdel $2
echo "User [$2] del successful”
else
echo “The user [$2] is not exist.”
fi
for((num=1;num<=$4;num++));do
id ${3} ${num} >>/dev/null 2&1
if [ $? -ne 0 ];then
Userdel
3
{3}
3{num}
echo “user [
3
{3}
3{num} delete successfully”
else
echo “the user [
3
{3}
3{num}] is not exist”
fi
else
echo “Parameter error!Please input [add] or [del].”
exit 10086
fi
else
Usage
fi
cat-c --characters=LIST
select only these characters