shell指令碼案例(一)批量建立使用者

2021-09-12 07:46:00 字數 4337 閱讀 7240

案例需求

用shell指令碼批量建立linux使用者

實現要求:建立使用者student1到student50,指定組為student組!而且每個使用者需要設定乙個不同的密碼!

實現指令碼

#!/bin/bash

grep 'student' /etc/group >/dev/null 2>/dev/null

[ $? -eq 0 ] || groupadd student >/dev/null 2>/dev/null

for i in `seq 50`

do id student$i >/dev/null 2>/dev/null;

[ $? -eq 0 ] && echo -e "[\033[31merror\033[0m]user student$i is exist." && continue;

useradd -g student student$i >/dev/null 2>/dev/null;

pass=`echo "$random$random"`;

[ $? -eq 0 ] && echo -e "[\033[32mok\033[0m]user student$i create success." && echo "$pass" | passwd student$i --stdin >/dev/null 2>/dev/null;

[ $? -eq 0 ] && echo "student$i $pass" >> userlist.txt

done

執行結果

[ok]user student1 create success.

[ok]user student2 create success.

[ok]user student3 create success.

[ok]user student4 create success.

[ok]user student5 create success.

[ok]user student6 create success.

[ok]user student7 create success.

[ok]user student8 create success.

[ok]user student9 create success.

[ok]user student10 create success.

[ok]user student11 create success.

[ok]user student12 create success.

[ok]user student13 create success.

[ok]user student14 create success.

[ok]user student15 create success.

[ok]user student16 create success.

[ok]user student17 create success.

[ok]user student18 create success.

[ok]user student19 create success.

[ok]user student20 create success.

[ok]user student21 create success.

[ok]user student22 create success.

[ok]user student23 create success.

[ok]user student24 create success.

[ok]user student25 create success.

[ok]user student26 create success.

[ok]user student27 create success.

[ok]user student28 create success.

[ok]user student29 create success.

[ok]user student30 create success.

[ok]user student31 create success.

[ok]user student32 create success.

[ok]user student33 create success.

[ok]user student34 create success.

[ok]user student35 create success.

[ok]user student36 create success.

[ok]user student37 create success.

[ok]user student38 create success.

[ok]user student39 create success.

[ok]user student40 create success.

[ok]user student41 create success.

[ok]user student42 create success.

[ok]user student43 create success.

[ok]user student44 create success.

[error]user student45 is exist.

[ok]user student46 create success.

[error]user student47 is exist.

[error]user student48 is exist.

[error]user student49 is exist.

[error]user student50 is exist.

輸出檔案

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 ...

Py修仙路(一)批量建立資料夾

處 在import os file open r c users wym desktop peirong all.txt r line file readlines for title in line title title.strip n path c users wym desktop peir...

企業級shell指令碼之二 批量建立使用者

背景 生產中當系統管理人員比較多,存在批量建立使用者的情況 指令碼比較簡單 如下 bin bash echo we will batch creating user read p pls enter your prefix and number prefix num if z pre fix o z...