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

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

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

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

脚本运行

  1. [root@arppinging scripts]# bash rmuser.sh
  2. [ok] delete user student1 succes.
  3. [ok] delete user student2 succes.
  4. [ok] delete user student3 succes.
  5. [ok] delete user student4 succes.
  6. [ok] delete user student5 succes.
  7. [ok] delete user student6 succes.
  8. [ok] delete user student7 succes.
  9. [ok] delete user student8 succes.
  10. [ok] delete user student9 succes.
  11. [ok] delete user student10 succes.
  12. [ok] delete user student11 succes.
  13. [ok] delete user student12 succes.
  14. [ok] delete user student13 succes.
  15. [ok] delete user student14 succes.
  16. [ok] delete user student15 succes.
  17. [ok] delete user student16 succes.
  18. [ok] delete user student17 succes.
  19. [ok] delete user student18 succes.
  20. [ok] delete user student19 succes.
  21. [ok] delete user student20 succes.
  22. [ok] delete user student21 succes.
  23. [ok] delete user student22 succes.
  24. [ok] delete user student23 succes.
  25. [ok] delete user student24 succes.
  26. [ok] delete user student25 succes.
  27. [ok] delete user student26 succes.
  28. [ok] delete user student27 succes.
  29. [ok] delete user student28 succes.
  30. [ok] delete user student29 succes.
  31. [ok] delete user student30 succes.
  32. [ok] delete user student31 succes.
  33. [ok] delete user student32 succes.
  34. [ok] delete user student33 succes.
  35. [ok] delete user student34 succes.
  36. [ok] delete user student35 succes.
  37. [ok] delete user student36 succes.
  38. [ok] delete user student37 succes.
  39. [ok] delete user student38 succes.
  40. [ok] delete user student39 succes.
  41. [ok] delete user student40 succes.
  42. [ok] delete user student41 succes.
  43. [ok] delete user student42 succes.
  44. [ok] delete user student43 succes.
  45. [ok] delete user student44 succes.
  46. [ok] delete user student46 succes.
  47. [root@arppinging scripts]#

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

  1. [root@arppinging scripts]# bash rmuser.sh
  2. [error] user student1 not exist.
  3. [error] user student2 not exist.
  4. [error] user student3 not exist.
  5. [error] user student4 not exist.
  6. [error] user student5 not exist.
  7. [error] user student6 not exist.
  8. [error] user student7 not exist.
  9. [error] user student8 not exist.
  10. [error] user student9 not exist.
  11. [error] user student10 not exist.
  12. [error] user student11 not exist.
  13. [error] user student12 not exist.
  14. [error] user student13 not exist.
  15. [error] user student14 not exist.
  16. [error] user student15 not exist.
  17. [error] user student16 not exist.
  18. [error] user student17 not exist.
  19. [error] user student18 not exist.
  20. [error] user student19 not exist.
  21. [error] user student20 not exist.
  22. [error] user student21 not exist.
  23. [error] user student22 not exist.
  24. [error] user student23 not exist.
  25. [error] user student24 not exist.
  26. [error] user student25 not exist.
  27. [error] user student26 not exist.
  28. [error] user student27 not exist.
  29. [error] user student28 not exist.
  30. [error] user student29 not exist.
  31. [error] user student30 not exist.
  32. [error] user student31 not exist.
  33. [error] user student32 not exist.
  34. [error] user student33 not exist.
  35. [error] user student34 not exist.
  36. [error] user student35 not exist.
  37. [error] user student36 not exist.
  38. [error] user student37 not exist.
  39. [error] user student38 not exist.
  40. [error] user student39 not exist.
  41. [error] user student40 not exist.
  42. [error] user student41 not exist.
  43. [error] user student42 not exist.
  44. [error] user student43 not exist.
  45. [error] user student44 not exist.
  46. [error] user student46 not exist.
  47. [root@arpinging scripts]#

查看保存的文件

  1. [root@arppinging scripts]# cat userlist.txt
  2.  
  3. [root@arppinging scripts]#

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

猜你在找的Bash相关文章