脚本中90%以上是我自己写,只有添加账号的核心部分是我在募课网上参考的,然后再修改再修改,或者是不停地增加再增加,有兴趣可以拿来玩玩。
目前该脚本只在centos6.7上测试通过,其他版本未测试过。
#!/bin/bash b=0 n=0 countK=0 while [ $[$countK] -eq 0 ] do echo "1.批量添加用户" echo "2.批量删除用户" read -p "请选择:" -t 60 selectK if [ $[$selectK] -eq 1 ];then read -p "请输入要批量添加的用户名: " -t 30 name read -p "请输入要添加的用户数量: " -t 30 num read -p "请给出一个通用密码: " -t 30 pass if [ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ];then y=$(echo $num | sed 's/[0-9]//g') if [ -z "$y" ];then for i in $( cat /etc/passwd|grep "/bin/bash"|grep $name|cut -d":" -f 1 ) do for (( a=1;a<=$num;a=a+1 )) do if [ $i == $name$a ];then keepa[a-1]=$name$a b=$[$a+1] break fi done done if [ $[$b] -eq 0 ];then b=1 fi for (( h=$[$num-$[$num-$b]];h<=$num;h=$h+1 )) do c=0 for g in $( cat /etc/passwd|grep "/bin/bash"|grep $name|cut -d":" -f 1 ) do if [ $g == $name$h ];then c=1 fi done if [ $c == 0 ];then keepx[$n]=$name$h n=$[$n+1] fi done echo "-------已经存在的用户数:" for v in ${keepa[@]} do echo $v done echo "-------不存在的用户数:" for keeplength in ${keepx[@]} do echo $keeplength done if [ ! -z $keeplength ];then echo "-------现在创建用户:" for u in ${keepx[@]} do /usr/sbin/useradd $u &> /dev/null usermod -s /bin/bash $u &> /dev/null echo $pass | /usr/bin/passwd --stdin $u &> /dev/null done echo "-------创建完成:" countK=1 else echo "没有可以创建的用户,因为所创建的用户可能已经存在。" countK=1 fi #if [ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ];then #y=$(echo $num | sed 's/[0-9]//g') #if [ -z "$y" ];then #for u in ${keepx[@]} #do # /usr/sbin/useradd $u &> /dev/null # usermod -s /bin/bash $u &> /dev/null # echo $pass | /usr/bin/passwd --stdin $u &> /dev/null # echo $u #done fi fi elif [ $[$selectK] -eq 2 ];then read -p "请输入要批量删除的用户账号:" -t 60 name read -p "请输入要删除的批量起始号:" -t 60 numS read -p "请输入要删除的指量结束号:" -t 60 numO numsS=$(echo $numS | sed 's/[0-9]//g') numoS=$(echo $numO | sed 's/[0-9]//g') #判断输入的账号是否存在,精确查找: if [ -z "$( cat /etc/passwd|grep "/bin/bash"|grep -v "root"|grep "[^a-zA-Z]$name[^a-zA-Z]"|grep [^0-9]$name|grep "[^.\$\_-]$name[^.\$\_-]"|cut -d":" -f 1 )" ];then echo "你输入的用户不存在!" echo " " countK=0 elif [ ! -z "$numsS" -o ! -z "$numoS" ];then echo "对不起,你输入的起始或结束号有误,请重新输入!" countK=0 else #如果不输入起始号和尾号的话,则删除所有账户: if [ -z "$numS" -a -z "$numO" ];then read -p "你输入的起始或结束号为空,是否删除$name全部用户?(y确定,n否定)" -t 60 y if [ $y == "y" ];then echo "开始删除$name全部用户:" #declare -a keepc n=0 #循环把所有账号都查询出来: for x in $( cat /etc/passwd|grep "/bin/bash"|grep -v "root"|grep "[^a-zA-Z]$name$[^a-zA-Z]"|grep "[^0-9]$name"|grep "$name[^0-9]"|grep "[^.\$\_-]$name[^.\$\_-]"|cut -d":" -f 1 ) do userdel -r $x done echo "用户$name删除完成" countK=1 elif [ $y == "n" ];then countK=0 fi elif [ ! -z "$numS" -a -z "$numO" ];then #如果只输入起始号而不输入结束号的话,则从起始号开始进行删除: read -p "你输入的起始号不为空,但结束号为空,所以从起始号开始删除可否?(y确定,n否定)" -t 60 y countK=0 if [ -z "$( cat /etc/passwd|grep "/bin/bash"|grep -v "root"|grep "[^a-zA-Z]$name$numS[^a-zA-Z]"|grep "[^0-9]$name$numS"|grep "$name$numS[^0-9]"|grep "[^.\$\_-]$name$numS[^.\$\_-]"|cut -d":" -f 1 )" ];then echo "你输入的起始号可能存在错误,原因可能是不存在该起始号的账号。" else #定义数组保存尾号数: declare -a keepc if [ $y == "y" ];then h=0 #使用cat查询出被查询账号的所有数量: for x in $( cat /etc/passwd|grep "/bin/bash"|grep -v "root"|grep "[^a-zA-Z]$name[^a-zA-Z]"|grep "[^0-9]$name"|grep "[^.\$\_-]$name[^.\$\_-]"|cut -d":" -f 1 ) do #keepc[$h]=${x:$[${#x}-1]:$[${#x}-1]} #h=$[$h+1] #把查询出来的账号尾号放到数组里去 #找出字符串最后一次出现的位置,然后截取从该位置后的字符,这里就是截取尾号。 keepc[$h]=${x##*$name} h=$[$h+1] done #循环把所有尾号都查询出来 for m in ${keepc[@]} do #删除大于起始号的账号 if [ $m -ge $numS ];then userdel -r $name$m echo "$name$m删除成功" fi done countK=1 elif [ $y == "n" ];then countK=0 fi fi elif [ -z "$numS" -a ! -z "$numO" ];then #起始号为空结束号不为空的判断。 read -p "你输入的起始号为空,但结束号为$numO,删除结束号之前的账号可否?(y确定,n否定)" -t 60 y countK=0 if [ -z "$( cat /etc/passwd|grep "/bin/bash"|grep -v "root"|grep "[^a-zA-Z]$name$numO[^a-zA-Z]"|grep "[^0-9]$name$numO"|grep "$name$numO[^0-9]"|grep "[^.\$\_-]$name$numO[^.\$\_-]"|cut -d":" -f 1 )" ];then echo "你输入的结束号可能存在错误,原因可能是不存在该结束号的账号。" else declare -a keepc if [ $y == "y" ];then h=0 #和上面一样,先把所有账号都查询出来: for x in $( cat /etc/passwd|grep "/bin/bash"|grep -v "root"|grep "[^a-zA-Z]$name[^a-zA-Z]"|grep "[^0-9]$name"|grep "[^.\$\_-]$name[^.\$\_-]"|cut -d":" -f 1 ) do #保存在数组里: keepc[$h]=${x##*$name} h=$[$h+1] done #循环把所有尾号都查询出来 for m in ${keepc[@]} do #删除大于起始号的账号 if [ $m -le $numO ];then userdel -r $name$m echo "$name$m删除成功" fi done countK=1 elif [ $y == "n" ];then countK=0 fi fi else #最后判断删除指定范围内账号: countK=0 g=0 declare -a keepd #首先判断一下输入的结束结是否超过范围 #下面的算法其实完全没有必要写,判断是否超出范围的方法很简单,可以用sort来判断,或者直接查询输入结束号的账号是否存在就行了 #我只是练练我以学过的算法而已 #循环查找出所有账号的尾号 for j in $( cat /etc/passwd|grep "/bin/bash"|grep -v "root"|grep "[^a-zA-Z]$name[^a-zA-Z]"|grep "[^0-9]$name"|grep "[^.\$\_-]$name[^.\$\_-]"|cut -d":" -f 1 ) do keepd[$g]=${j##*$name} g=$[$g+1] done #这个算法就是排序,把尾号小的放到尾号大的前面 #该算法可以在百度上查到 for (( i=0;i<$g;i=i+1 )) do for (( j=$i;j<$g;j=j+1 )) do if [ ${keepd[$i]} -gt ${keepd[$j]} ];then #keepTy是临时保存用的 keepTy=${keepd[$i]} keepd[$i]=${keepd[$j]} keepd[$j]=$[$keepTy] fi done done if [ $numO -gt ${keepd[$g-1]} ];then #判断结束号是否过大 echo "输入的结束号过大,请重新输入。" elif [ $numO -lt $numS ];then #判断起始号是否大于结束号 echo "输入的起始号大于结束号,请重新输入。" elif [ -z "$( cat /etc/passwd|grep "/bin/bash"|grep -v "root"|grep "[^a-zA-Z]$name$numS[^a-zA-Z]"|grep "[^0-9]$name$numS"|grep "[^.\$\_-]$name$numS[^.\$\_-]"|cut -d":" -f 1 )" ];then #判断起始号是否存在 echo "该起始号的账号不存在。" elif [ -z "$( cat /etc/passwd|grep "/bin/bash"|grep -v "root"|grep "[^a-zA-Z]$name$numO[^a-zA-Z]"|grep "[^0-9]$name$numO"|grep "[^.\$\_-]$name$numO[^.\$\_-]"|cut -d":" -f 1 )" ];then #判断结束号是否存在 echo "该结束号的账号不存在。" else declare -a keepc c1=0 c2=0 countC=0 h=0 #查询出所有账号,然后截出尾号: for x in $( cat /etc/passwd|grep "/bin/bash"|grep -v "root"|grep "[^a-zA-Z]$name[^a-zA-Z]"|grep "[^0-9]$name"|grep "[^.\$\_-]$name[^.\$\_-]"|cut -d":" -f 1 ) do #保存在数组里: keepc[$h]=${x##*$name} h=$[$h+1] done #处理起始号和尾号区间代码 for m in ${keepc[@]} do if [ $m == $numS ];then c1=$countC fi if [ $m == $numO ];then c2=$countC fi countC=$[$countC+1] done for (( i=$c1;i<=$c2;i=i+1 )) do userdel -r $name${keepc[$i]} echo "$name${keepc[$i]}删除完成" done countK=1 fi fi fi else echo "你的选择有错误,请重新选择。" countK=0 fi done