Shell script case (i) batch create user

Source: Internet
Author: User

Case requirements
Batch build Linux users with shell scripts
Implementation requirements: Create user student1 to Student50, specify Group as student group! And each user needs to set a different password!

Implementing scripts

#!/bin/bashgrep ‘student‘ /etc/group >/dev/null 2>/dev/null[ $? -eq 0 ] || groupadd student >/dev/null 2>/dev/nullfor 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.txtdone

Execution results

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

Output File

 [[email protected] scripts]# cat userlist.txt student1 2114716572student2 2114210679student3 247707542student4 24285573student5 6352319student6 3218029353student7 208547266student8 52665904student9 247106006st Udent10 57387229student11 2119910618student12 2616717096student13 425723665student14 3180432159student15 184123909st Udent16 196088448student17 1308115588student18 2383911515student19 2745018771student20 269562862student21 1124524793 student22 469429790student23 14215545student24 953612543student25 2362926724student26 1046118927student27 2753416900 Student28 677132365student29 640032326student30 1096512512student31 856828828student32 1528420840student33 69394475s Tudent34 2948020145student35 2181024297student36 706618857student37 786028997student38 111793209student39 3091930571 STUDENT40 241441040student41 136041083student42 2902517667student43 320702112student44 40616442student46 867923907[[ Email protected] scripts]#  

Shell script case (i) Bulk user creation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.