Shell script Batch Add user

Source: Internet
Author: User

Yes, this is a simple script, do not write do not know, write to find out how many shortcomings.

The following script is mainly used to bulk add the user's script, originally very simple script, but added some judgment, to verify their own learning.

1. Determine if user input is incorrect

2. Determine if the user exists, the existence of skip no longer processing



The code is as follows:

#/bin/bash# Batch Add user script   #判断用户是否存在函数function  panduan ()     {         for nametmp in $ (cat /tmp/user.txt)              do                 if [  $nametmp  = $1 ]                     then                          return 6                         break                 fi             done    }cat /etc/passwd|awk -f :  ' {print $1} ' &>>/tmp /user.txt# Read user input information read -p  "Please enter the prefix:"  nameread -p  "Please enter the number:"  numread -p  " Please enter the initial password:  password  #判断输入是否为空if  [ -z  $name  -o -z  $num  -o  -z  "$password"  ]    then        echo   "Input cannot be empty"     else# determine whether the number of users is formatted as a number, exit      echo  $num without a number | grep ^[0-9]*$&>/dev/null    if [ $? -eq 0 ]         then             for i in  ' seq 1  $num '                  do  #判断用户是否存在                      panduan  $name $i&>/dev/null                     if [  $? -ne 6 ]                         then                          useradd  $name $i>>/dev/null                         echo  "$password" |passwd -- stdin  $name $i>>/dev/null                         echo  "User name: $name $i     Password: $password    Create success"                     else                          echo  "User $name$i exist, do not process"                                                 fi             done        else             echo  "Number of users entered incorrectly, please enter a number"     fi                  fi

Ask for advice, there is no easier way.

This article is from the "Tommy Lin" blog, so be sure to keep this source http://linyingyong.blog.51cto.com/6609544/1812382

Shell script Batch Add user

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.