shell脚本案例批量创建用户
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本案例批量创建用户相关的知识,希望对你有一定的参考价值。
案例需求
用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.
输出文件
[[email protected] scripts]# cat userlist.txt
student1 2114716572
student2 2114210679
student3 247707542
student4 24285573
student5 6352319
student6 3218029353
student7 208547266
student8 52665904
student9 247106006
student10 57387229
student11 2119910618
student12 2616717096
student13 425723665
student14 3180432159
student15 184123909
student16 196088448
student17 1308115588
student18 2383911515
student19 2745018771
student20 269562862
student21 1124524793
student22 469429790
student23 14215545
student24 953612543
student25 2362926724
student26 1046118927
student27 2753416900
student28 677132365
student29 640032326
student30 1096512512
student31 856828828
student32 1528420840
student33 69394475
student34 2948020145
student35 2181024297
student36 706618857
student37 786028997
student38 111793209
student39 3091930571
student40 241441040
student41 136041083
student42 2902517667
student43 320702112
student44 40616442
student46 867923907
[[email protected] scripts]#
以上是关于shell脚本案例批量创建用户的主要内容,如果未能解决你的问题,请参考以下文章