【1】shell脚本基础练习

该博客内容涉及使用Shell脚本批量创建用户并分配到特定组。原始脚本存在错误,如将用户编号以01开始,但实际上在调用时会忽略前导零。修正后的脚本解决了这个问题,并为每个新建用户设置了初始密码。执行结果显示,所有用户成功创建并设置了密码。
摘要由CSDN通过智能技术生成

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

练习代码:

#!/bin/bash
groupadd class1
for ((i=01; i<=30; i++))
do
    useradd stu$i -g class1
    echo "stu$i is created"
done

执行结果:

[root@localhost shell]# bash std.sh 
stu1 is created
stu2 is created
stu3 is created
stu4 is created
stu5 is created
stu6 is created
stu7 is created
stu8 is created
stu9 is created
stu10 is created
stu11 is created
stu12 is created
stu13 is created
stu14 is created
stu15 is created
stu16 is created
stu17 is created
stu18 is created
stu19 is created
stu20 is created
stu21 is created
stu22 is created
stu23 is created
stu24 is created
stu25 is created
stu26 is created
stu27 is created
stu28 is created
stu29 is created
stu30 is created
[root@localhost shell]# id stu1
uid=1004(stu1) gid=1004(class1) groups=1004(class1)

原因分析:

1、没有认识到调取i时候只会从有效数字开始,比如i=01,调取的结果是i=1

2、在看到脚本要求后思考不全面,创建的用户没有初始密码

最终代码:

#!/bin/bash
groupadd class1
for ((i=01; i<=30; i++))
do
    if [ $i -lt 10 ]
        then
        useradd stu0$i -g class1
        echo redhat | passwd --stdin stu0$i
    else
        useradd stu$i -g class1
        echo redhat | passwd --stdin stu$i
    fi
done

执行结果:

[root@localhost shell]# bash std.sh 
groupadd: group 'class1' already exists
Changing password for user stu01.
passwd: all authentication tokens updated successfully.
Changing password for user stu02.
passwd: all authentication tokens updated successfully.
Changing password for user stu03.
passwd: all authentication tokens updated successfully.
Changing password for user stu04.
passwd: all authentication tokens updated successfully.
Changing password for user stu05.
passwd: all authentication tokens updated successfully.
Changing password for user stu06.
passwd: all authentication tokens updated successfully.
Changing password for user stu07.
passwd: all authentication tokens updated successfully.
Changing password for user stu08.
passwd: all authentication tokens updated successfully.
Changing password for user stu09.
passwd: all authentication tokens updated successfully.
Changing password for user stu10.
passwd: all authentication tokens updated successfully.
Changing password for user stu11.
passwd: all authentication tokens updated successfully.
Changing password for user stu12.
passwd: all authentication tokens updated successfully.
Changing password for user stu13.
passwd: all authentication tokens updated successfully.
Changing password for user stu14.
passwd: all authentication tokens updated successfully.
Changing password for user stu15.
passwd: all authentication tokens updated successfully.
Changing password for user stu16.
passwd: all authentication tokens updated successfully.
Changing password for user stu17.
passwd: all authentication tokens updated successfully.
Changing password for user stu18.
passwd: all authentication tokens updated successfully.
Changing password for user stu19.
passwd: all authentication tokens updated successfully.
Changing password for user stu20.
passwd: all authentication tokens updated successfully.
Changing password for user stu21.
passwd: all authentication tokens updated successfully.
Changing password for user stu22.
passwd: all authentication tokens updated successfully.
Changing password for user stu23.
passwd: all authentication tokens updated successfully.
Changing password for user stu24.
passwd: all authentication tokens updated successfully.
Changing password for user stu25.
passwd: all authentication tokens updated successfully.
Changing password for user stu26.
passwd: all authentication tokens updated successfully.
Changing password for user stu27.
passwd: all authentication tokens updated successfully.
Changing password for user stu28.
passwd: all authentication tokens updated successfully.
Changing password for user stu29.
passwd: all authentication tokens updated successfully.
Changing password for user stu30.
passwd: all authentication tokens updated successfully.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亲爱的老少女

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值