1.批量建立用户,随机生成10位数字字母组合的密码,并将密码保存到userinfo.txt中。
#!/bin/bash
root_uid=0
if [ $UID -ne $root_uid ]
then
echo "Only root can run this script."
else
read -p "please input the name and the number you want to add:" name number
for((i=1;i<=$number;i++))
do
password=$(cat /dev/urandom | head -n 10 | md5sum | head -c 10)
useradd $name$i
echo "$password" | passwd --stdin $i &> /dev/null
echo "$name$i $password" >> userinfo.txt
echo "$name$i is added"
done
fi
运行结果;
2.批量删除用户及其主目录。
#!/bin/bash
root_uid=0
if [ $UID -ne $root_uid ]
then
echo "only root can run thid script."
else
read -p "please input the username and the number you want to delete:" name number
for ((i=1; i<=$number; i++))
do
userdel -r $name$i
pwconv && echo "$name$i is deleted"
done
fi
运行结果:
本文出自 “11849650” 博客,谢绝转载!