OpenEuler 第八章《使用shell脚本》思考题

新建用户列表文件userlist,在文件中按行写入10个用户名。新建一个useradd.sh 脚本,要求该脚本能自动创建用户,用户名为userlist列表中的用户,密码为openEuler12#$,新创建的用户家目录内包含一个hello.txt文件,hello.txt文件的归属用户和私有组为该用户。

新建10个用户 a[1-10]

[root@openEuler ~]# vim userlist
[root@openEuler ~]# cat userlist 
a1
a2
a3
a4
a5
a6
a7
a8
a9
a10

创建useradd.sh脚本

[root@openEuler ~]# vim useradd.sh

脚本代码

#!/bin/bash

for user_name in $(cat ./userlist)
do
	echo $user_name
	useradd $user_name
	echo 'openEuler12#$' | passwd $user_name --stdin  &>/dev/null
	file="/home/$user_name/hello.txt"
	mkdir $file
	chown $user_name $file
	chgrp $user_name $file
done

检查结果

[root@openEuler ~]# tail -10 /etc/passwd
a1:x:1004:1004::/home/a1:/bin/bash
a2:x:1005:1005::/home/a2:/bin/bash
a3:x:1006:1006::/home/a3:/bin/bash
a4:x:1007:1007::/home/a4:/bin/bash
a5:x:1008:1008::/h
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值