shell指令碼案例(二)批量刪除使用者

2021-09-12 07:46:00 字數 4609 閱讀 5379

案例-接著上乙個案例

希望批量刪除上個案例建立的所有使用者

指令碼展示:(下次再解釋)

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

檢視儲存的檔案

shell指令碼案例

bin bash for迴圈的使用 for num in 1 2 3 4 5 6 do echo num done bin bash a whoami read p 請輸入想要驗證的使用者名稱 b if b a then echo 是當前使用者 else echo 不是當前使用者,需示警 fi bi...

shell指令碼案例賞析

bin bash 用法 rebatch.sh 截止到月份的日期 例如 rebatch.sh 2014 06 etc profile bashrc arg 1 start date 01 end date 01 count 1 cat dev null log file db2 o connect t...

shell指令碼案例(提供思路)

1 define the execution environment of the script 定義指令碼的執行環境 執行指令碼用英文 usr bin bash 2 號代表注釋 是特例 3 指令碼資訊 author tang xiaoming created time 2019 05 07 11 ...