Interactively add user scripts under Linux (four examples)

Source: Internet
Author: User
Tags stdin

Recently need to use the Linux script Interactive way to add users, their own three scripts to share and record, easy to follow-up, but also share, I hope to help the same shell script rookie.

Example one: Add users in an elegant way

Follow the prompts, enter the user you want to add, if the user exists, then prompt, exit the script;

#!/bin/sh# This scripts was created by miaocbin# qq:289303323# blog:http://miaocbin.blog.51cto.com# Elegant Way to add users: if the user exists, The prompt already exists, and exits, if it does not exist, increases and sets the password; echo-n "Please input your username:" Read usernamegrep "$username"/etc/passwd >/dev/null  2>&1 if [$?-eq 0];  Then echo $username exist.  Exit 0 Else Echo $username is not exist. echo "Now create user $username"/usr/sbin/useradd $username echo-n "Please input your password:" Read password echo "${password}123" |passwd--stdin $username echo "User $username has been added,the password is ${password}123" fi

Example two: Adding users in an elegant way

Similar to the example, only the implementation of different ways, it can be found that all roads in Linux to Rome, a variety of ways to achieve the need for continuous research, to find the right one is the best.

#!/bin/sh# This scripts was created by miaocbin# qq:289303323# blog:http://miaocbin.blog.51cto.com# Elegant way to add user Echo-n " Please input your username: "Read Usernameif cat/etc/passwd | Awk-f: ' {print $} ' | grep $username >/dev/null 2>&1 then echo "User $username already exists" Else/usr/sbin/useradd $username Ech O-n "Enter your password:" Read password echo "${password}123" |passwd--stdin $username echo "User $username have bee n added,the password is ${password}123 "fi

Example three: Add users gracefully (simple, recommended)

#!/bin/sh# this scripts is created by  miaocbin# qq:289303323# blog:http://miaocbin.blog.51cto.com# elegant way to add user echo -n  "Please  Input your username: "read usernameid  $username  >/dev/null 2>&1if  [ $? -ne 0 ] ;  then  echo  $username  is not  exist.  echo  "now create user  $username"   /usr/sbin/useradd $ username  echo -n  "Please input your password:"   read  password  echo  "${password}123"  |passwd --stdin  $username   echo  " user  $username  have been added,the password is ${password}123 " else   echo  $username  exist.  exit 0fi 

Example four: Violent ways to add users (use caution!) )

Delete the user you entered first, then add again and set the password, use this script with caution in your production environment, and this script will recursively delete the user and all of its associated data.

#!/bin/sh# This scripts are created by miaocbin# qq:289303323# blog:http://miaocbin.blog.51cto.com# violence way to add users, such as user presence, how to delete after Re-add Echo-n "Please input your username:" Read usernamegrep "$username"/etc/passwd >/dev/null 2>&1 if [$?  -eq 0];  Then echo $username exist. echo "Now delete the user $username and readd the user $username"/usr/sbin/userdel-r $username/usr/sbin/useradd $user Name Echo-n "Please input your password:" Read password echo "${password}123" |passwd--stdin $username echo "User $u Sername has been added,the password is ${password}123 "fi

This article is from the "Cold Tea" blog, please be sure to keep this source http://miaocbin.blog.51cto.com/689091/1660080

Interactively add user scripts under Linux (four examples)

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.