2.设计一个shell程序,添加一个新组为class1,然后添加属于这个组的30个用户,用户名的形式为stdxx,其中xx从01到30。
思路:
先需要创建一个组,组名是class1
接着创建用户,(i=1, i<=30, i=i+1)
用户名有两种情况 stu0${i} (i <=9) stu{i} (i>9)
接着需要创建用户家目录,修改用户属组
代码如下:
#! /bin/bash
# new a class1 and useradd 30 users,The user name such as stuxx,from 1 to 30
i=1
groupadd class1
while [ $i -le 30 ]
do
if [ $i -le 9 ] ;then
USERNAME=stu0${i}
else
USERNAME=stu${i}
fi
useradd $USERNAME
mkdir -p /home/$USERNMAE
chown -R $USERNAME /home/$USERNAME
usermod -g class1 $USERNAME
i=$(($i+1))
done
查看结果
[root@ecs 30]# cat /etc/passwd | tail -n 30
stu01:x:1004:1004::/home/stu01:/bin/bash
stu02:x:1005:1004::/home/stu02:/bin/bash
stu03:x:1006:1004::/home/stu03:/bin/bash
stu04:x:1007:1004::/home/stu04:/bin/bash
stu05:x:1008:1004::/home/stu05:/bin/bash
stu06:x:1009:1004::/home/stu06:/bin/bash
stu07:x:1010:1004::/home/stu07:/bin/bash
stu08:x:1011:1004::/home/stu08:/bin/bash
stu09:x:1012:1004::/home/stu09:/bin/bash
stu10:x:1013:1004::/home/stu10:/bin/bash
查看用户属组:
[root@ecs 30]# groups stu01
stu01 : class1
################################
3.编写shell程序,实现自动删除30个账号的功能。账号名为stud01至stud30。
删除刚才创建的30个递归用户
代码如下
#! /bin/bash
# del some user for make a mistake
i=1
while [ $i -le 30 ]
do
if [ $i -le 9 ] ;then
userdel -r stu0${i}
else
userdel -r stu${i}
fi
i=$(($i+1))
done
~