shell脚本基础练习题

1.设计一个 shell 程序,添加一个新组为 class1,然后添加属于这个组的 30 个用户,用户名的形式为stuxx,其中 xx 从 01 到 30。

在/tmp目录下新建一个shell脚本std.sh
[root@localhost tmp]# vim std.sh

#!/bin/bash
#添加一个组class1
groupadd class1;
for((i=1;i<=30;i++))
    do
        if [ $i -lt 10 ]
            then
            username=stu0${i}
        else
            username=stu${i}
        fi
        useradd -g class1 $username
#将密码设为123且每个用户都可用
        echo 123 | passwd --stdin $username
    done

给stud.sh设置权限,并运行脚本
[root@localhost tmp]# chmod +x std.sh
[root@localhost tmp]# ./std.sh
运行结果如下:(未显示完全)
运行结果

2.编写 shell 程序,实现自动删除 30 个账号的功能。账号名为 stu1 至 stu30。
前提:得创建30个stu1至stu30的用户(题1.已创建)。
在/tmp目录下新建一个shell脚本stud.sh
[root@localhost tmp]# vim stud.sh

#!/bin/bash
for((i=1;i<=30;i++))
    do
        if [ $i -lt 10 ]
            then
            userdel -r stu0${i} 2>/dev/null
            echo "stu0${i} delete successfully"
        else
            userdel -r stu${i} 2>/dev/null
            echo "stu${i} delete successfully"
        fi
done

给stud1.sh设置权限,并运行脚本
[root@localhost tmp]# chmod +x stud.sh
[root@localhost tmp]# ./stud.sh
运行结果如下:(未显示完全)
运行结果


参考题目https://blog.csdn.net/Rookie_hh/article/details/78203883

猜你喜欢

转载自blog.csdn.net/wxc_qlu/article/details/82392411