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


运行结果;

wKiom1jcvgfD69CbAAAYil1q_sg964.png-wh_50

wKiom1jcvhaTrJc6AAA2iB-RydY648.png-wh_50

wKiom1jcviaCHJWTAAAz1VC3sBY664.png-wh_50

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


运行结果:

wKiom1jcvz3jXt4PAAAiP0RkCUA339.png-wh_50

wKiom1jcvz3ifuCSAAALoXushEY993.png-wh_50