首页 > 代码库 > 批量创建用户和删除用户

批量创建用户和删除用户

wKiom1O_vkizvESyAAM0r_MAIzQ550.jpgwKiom1O_vt3jCcJSAARNuRzA9p4340.jpg


批量创建用户

read -p "请输入用户名的前缀:" user
read -p "请输入用户的数目:" num
count=0
if (($num>10))
then
        echo "最多只能同时新建10个用户"
else
        for i in $(seq $num)
        do
                if useradd $user$i
                then
                        count=$(($count+1))
                        echo "用户$user$i已经创建成功"
                fi
        done
        echo "一共新建的用户数:$count个"
fi

批量删除用户

read -p "请输入需要删除的用户名前缀:" user
if [[ $user = "" ]]
then
        echo "请输入合法的用户名前缀"
else
        if ! cat /etc/passwd |grep ^$user &>/dev/null
        then
                echo "以$user开头的用户不存在"
        else
                awk -F ":" ‘$3<500||$3>60000{print $1}‘ /etc/passwd|grep ^$user>user2.txt
                user2=($(cat user2.txt))
                for j in ${user2[@]}
                do
                        echo "$j是系统用户不能删除"
                done
                awk -F ":" ‘$3>=500{print $1}‘ /etc/passwd|grep ^$user>user1.txt
                user1=($(cat user1.txt))
                for i in ${user1[@]}
                do
                        userdel -r $i
                        echo "用户$i已经被成功删除"
                done
                echo "一共删除的用户数:${#user1[@]}"
        fi
fi

本文出自 “爱武装一辈子” 博客,请务必保留此出处http://menglinux.blog.51cto.com/8733806/1437220