shell脚本案例(二)批量删除用户

前端之家收集整理的这篇文章主要介绍了shell脚本案例(二)批量删除用户前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

案例-接着上一个案例
希望批量删除上个案例创建的所有用户
脚本展示:(下次再解释)

#!/bin/bash
echo > userlist.txt;
for i in `seq 50`
do
  id student$i >/dev/null 2>/dev/null;
  [ $? -ne 0 ] && echo -e "[\033[31merror\033[0m] user student$i not exist." && continue;
  userdel -r student$i >/dev/null 2>/dev/null;
  [ $? -eq 0 ] && echo -e "[\033[32mok\033[0m] delete user student$i succes."
done

脚本运行

[root@arppinging scripts]# bash rmuser.sh 
[ok] delete user student1 succes.
[ok] delete user student2 succes.
[ok] delete user student3 succes.
[ok] delete user student4 succes.
[ok] delete user student5 succes.
[ok] delete user student6 succes.
[ok] delete user student7 succes.
[ok] delete user student8 succes.
[ok] delete user student9 succes.
[ok] delete user student10 succes.
[ok] delete user student11 succes.
[ok] delete user student12 succes.
[ok] delete user student13 succes.
[ok] delete user student14 succes.
[ok] delete user student15 succes.
[ok] delete user student16 succes.
[ok] delete user student17 succes.
[ok] delete user student18 succes.
[ok] delete user student19 succes.
[ok] delete user student20 succes.
[ok] delete user student21 succes.
[ok] delete user student22 succes.
[ok] delete user student23 succes.
[ok] delete user student24 succes.
[ok] delete user student25 succes.
[ok] delete user student26 succes.
[ok] delete user student27 succes.
[ok] delete user student28 succes.
[ok] delete user student29 succes.
[ok] delete user student30 succes.
[ok] delete user student31 succes.
[ok] delete user student32 succes.
[ok] delete user student33 succes.
[ok] delete user student34 succes.
[ok] delete user student35 succes.
[ok] delete user student36 succes.
[ok] delete user student37 succes.
[ok] delete user student38 succes.
[ok] delete user student39 succes.
[ok] delete user student40 succes.
[ok] delete user student41 succes.
[ok] delete user student42 succes.
[ok] delete user student43 succes.
[ok] delete user student44 succes.
[ok] delete user student46 succes.
[root@arppinging scripts]#

如果不存在(再删除一次)

[root@arppinging scripts]# bash rmuser.sh 
[error] user student1 not exist.
[error] user student2 not exist.
[error] user student3 not exist.
[error] user student4 not exist.
[error] user student5 not exist.
[error] user student6 not exist.
[error] user student7 not exist.
[error] user student8 not exist.
[error] user student9 not exist.
[error] user student10 not exist.
[error] user student11 not exist.
[error] user student12 not exist.
[error] user student13 not exist.
[error] user student14 not exist.
[error] user student15 not exist.
[error] user student16 not exist.
[error] user student17 not exist.
[error] user student18 not exist.
[error] user student19 not exist.
[error] user student20 not exist.
[error] user student21 not exist.
[error] user student22 not exist.
[error] user student23 not exist.
[error] user student24 not exist.
[error] user student25 not exist.
[error] user student26 not exist.
[error] user student27 not exist.
[error] user student28 not exist.
[error] user student29 not exist.
[error] user student30 not exist.
[error] user student31 not exist.
[error] user student32 not exist.
[error] user student33 not exist.
[error] user student34 not exist.
[error] user student35 not exist.
[error] user student36 not exist.
[error] user student37 not exist.
[error] user student38 not exist.
[error] user student39 not exist.
[error] user student40 not exist.
[error] user student41 not exist.
[error] user student42 not exist.
[error] user student43 not exist.
[error] user student44 not exist.
[error] user student46 not exist.
[root@arpinging scripts]#

查看保存的文件

[root@arppinging scripts]# cat userlist.txt 

[root@arppinging scripts]#

欢迎关注的我的个人微信公众号

原文链接:https://www.f2er.com/bash/388054.html

猜你在找的Bash相关文章