shell 编程 批量添加用户


直接上代码,呃,声明一下,仅供测试,如果雷同,不胜荣幸

保存为任意文件名:给x权限

不加任何参数,则为添加user1.user2.user3....user10,密码=用户名

加参数例:command -a test 20  命令 -a添加 test是用户头 20是添加数量(一次添加最多不得大于100个,下面代码可以自己改)

command -d test 删除系统中用户名以test开头的,后跟数字的用户名,不加test,则默认删除user1.user2.user3...这类用户...慎用...

#!/bin/bash
U=${2:-user}
Num=${3:-10}
Res=`grep -o "^\<$U[0-9]\{1,\}\>" /etc/passwd|grep -o "[0-9]\{1,\}"|sort -n|tail -1`
Res1=${Res:-0}
Res2=$((Res1+Num))
ALL=`cat /etc/passwd|wc -l`
if [[ -z $1 || $1 == '-a' && ! -z $2 && $3 -le 100 ]];then
        for((i=${Res1};i<=${Res2};i++))
        do 
                if [[ $i -ne 0 ]];then
                        useradd  ${U}$i &> /dev/null
                        echo "${U}$i"|passwd --stdin ${U}$i &> /dev/null
                fi 
        done
                AALL=`cat /etc/passwd|wc -l`
                echo "Add user number is $((AALL-ALL))"
elif [[ ! -z $1 && "$1" = '-d' ]] ;then
        for((i=${Res1};i>=1;i--))
        do 
                Res3=`grep -o "^\<${U}${i}\>" /etc/passwd`

                if [[ -n $Res3 && "${U}$i" == "$Res3" ]];then
                        userdel -r  ${U}$i &> /dev/null
                fi
        done
                DALL=`cat /etc/passwd|wc -l`
                echo "Del user number is $((ALL-DALL))"
else
        echo -e "\033[33;5merror!\033[0m Please input none or -a or -d"
fi

相关内容