新建用户列表文件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::/home/a5:/bin/bash
a6:x:1009:1009::/home/a6:/bin/bash
a7:x:1010:2473::/home

本文介绍了如何创建一个名为userlist的文件,列出10个用户名,并编写一个useradd.sh脚本。该脚本用于自动创建这些用户,设置密码为'openEuler12#$',并确保每个新用户的家目录中包含一个由用户自身拥有和所属组的hello.txt文件。
最低0.47元/天 解锁文章
1524

被折叠的 条评论
为什么被折叠?



