Shell script Add or remove user and command use method

Source: Internet
Author: User
Tags stdin

Shell Requirements: Write a script

One, add 10 users user1 to User10, password with the user name, if the user exists, but requires only the user does not exist in the case can be added, in the format of/useradd.sh

Solution ideas: 1. Use the FOR Loop statement to add the user User1 to users 10

2, to determine whether the user exists, if present, the echo user already exists, if not present, add the user, and set the password and user name is the same.

Scripting: Vim useradd.sh and gives +x permissions,

#!/bin/bash#for i in{1..10};d o     if  id user$I >>/dev/null;then        echo  " User user$i   exits "     else         useradd user$I &> /dev/null         echo  "User user$i add sucefull"         echo  user$i | passwd --stdin user$i &>/dev/null         echo  "The user user$i passwd is user$i"       fidone 

Second, delete the User1 to User10, if the user does not exist, then the echo user does not exist, if the user exists, then delete. format is/userdel.sh

Ideas: 1, to determine whether the user exists, if any, then delete; if it does not exist, the user does not exist

2. Using the FOR Loop statement

Script: Vim userdel.sh, and gives +x permission

#!/bin/bash#for I in{1..10};d o if ID user$i >>/dev/null;then userdel-r user$i &>/dev/null     echo "User user$i Del Successful" else echo "user$i not exits." Fidone

Third, expand the above script, if add adds user user1 to User10, if del deletes the user user1 to User10. Formats such as: admin.sh--add |--del

Idea: 1, if no parameters are entered, the output format is incorrect and the correct input format is displayed, then exit

2, determine whether the input parameter is--add, and then determine whether the user name exists, if present, the display exists, if not, add User1 to User10 10 users, and configure the password is the same as the user name.

3, determine whether the input parameter is--del, and then determine whether the user name exists, if there is, delete the operation, and Echo shows that the deletion succeeded, if not, it shows that the user does not exist.

4, judge the input parameter is other, display the command error

Script: admin.sh, and give +x permissions

#!/bin/bash#if [ $# -lt 1 ];then     #未输入参数   echo  " Usage : useradd.sh [opention] "  exit 7fiif [ $1 ==  '--add '  ];then     #输入的第一个参数为   for i in {1..10};d o     if id user$i &>/dev/null ;then       echo   "User user$i exit"     else        useradd user$i       echo user$i |passwd --stdin  user$i &>/dev/null       echo  "User user$I add  successfull "     fi  doneelif [ $1 ==  '--del '  ];then  for i in {1..10};d o    if id user$i  &>/dev/null ;then        userdel -r user$i &>/dev/null        echo  "Userdel user$i  successful"      else      echo  "User user$i not exit"      fi doneelse  echo  "Exit"   exit 10fi

Third, expand the above script again, through the input parameters, to achieve the user's add and delete. Formats such as: admin2.sh--add|--del|--help Wli,jerry,tom,hello,hi

Ideas: 1, the input of the second parameter to convert to a list mode.

2. If the first parameter is--help, the help command is displayed

3. Other ideas follow the previous example

Script admin2.sh, and give +x permissions

#!/bin/bash#if [ $# -lt 1 ];then      echo  "Usage: Adminuser.sh --add|--del|--help [opentions] "  exit 7fiif [ $1 ==  '--add '  ];then for USER in  ' echo $2|sed  ' s/,/ /g ';d o    if id  $USER  &>/dev/null;then      echo  "$USER  exit "   else      useradd  $USER  &>/dev/ null      echo  $USER  |passwd --stdin  $USER  &>/ dev/null      echo  "add  $USER  finished."    fi doneelif [ $1 ==  '--del '  ];then  for user  in  ' echo $2|sed  ' s/,/ /g ';d o   if id  $USER  &>/ Dev/null;then      userdel -r  $USER  &>/dev/null      echo  "del  $USER  finished "   else      echo " $USER  not exit "    fi   doneelif [ $1 ==  '--help '  ];then   echo  "Usage :a.sh --add user1,user2,... | --del user1,user2,... | --help "else echo " Unknown atgin "fi


Command format:

1. Judgment Statement Format:

Single IF branch format:

if judgment condition; then

Fi

Dual-Branch Structure:

if judgment condition; then

Else

Fi

Multi-Branch Structure:

if judgment condition; then

elif ; Then

elif ; Then

Else

Fi

2. Circular statement format

For variable in list:d O

Loop body

Done

When the traversal is complete, exit

Build list: Echo $ |sed ' s/,//g '

List of integers:

{1..100} 1 to 100 integers

SEQ [Start number [step length]] End number

3. Add/Remove user formats

# useradd Create user

-U UID

-G GID (Basic Group)

-G GID .... (Additional groups)

-C "COMMENT" comment information

-D Specify home directory

-s Specifies the shell path/etc/shells: Specifies the security shell available in the current system

-m-k Force create home directory for users and copy shell files to home directory

-m/etc/login.defs, do not create home directory for users

-R Add System user

#userdel Delete a user

If no option is added, the user's home directory is not deleted by default

-R Delete user at the same time, delete home directory

4. passwd use Format

#passwd [USERNAME] Modify user password

--stdin

Example: Echo "Redhat" | passwd--stdin User3

- l lock

-U unlock

- d Delete user password


———————————————————— accumulate more, summarize more, practice more

This article is from the "Wish_" blog, be sure to keep this source http://wishliang.blog.51cto.com/11439802/1955966

Shell script Add or remove user and command use method

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.