新建用户列表文件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