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]# 

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

微信公众号二维码
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 210,978评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 89,954评论 2 384
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,623评论 0 345
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,324评论 1 282
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,390评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,741评论 1 289
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,892评论 3 405
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,655评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,104评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,451评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,569评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,254评论 4 328
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,834评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,725评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,950评论 1 264
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,260评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,446评论 2 348

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,734评论 25 707
  • 笃信自己在未来一定会变好,而且要120%的相信,即便有人打击掉你20%的信心,你也要依然用100%的信心把20%找...
    红茶配柠檬阅读 152评论 0 0
  • 今天看到新闻才知道一位伟大的物理学家霍金去世了, 同时也说到了今天是爱因斯坦的生日! 之前已经谈过314对我家族的...
    倩华老师逆向思维分享师阅读 709评论 0 0
  • 今天是2017年的最后一天了,也是我们思齐悦读派在2017的最后一天悦读 了。 然后我们今天就开起我们的悦读接龙。...
    庄涵涛_a497阅读 401评论 0 1
  • 八月十六日,闺女瞧娘日, 带箱好月饼,烧鸡买两只, 油条五六斤,鸡蛋一箱子, 一提纯牛奶,水果别忘记, 装到三轮上...
    林木成荫阅读 390评论 5 17