User creation of Linux scripts

Source: Internet
Author: User
Tags stdin egrep

Script Requirements :

1. Write a script createuser.sh, the execution syntax of the script must be:createuser.sh-u username-m password, options and parameters can support multiple spaces, but can not be reversed in order. When the correct option or parameter is not specified, exit the script with the error output prompt "Createuser.sh-u username-m password".
2. The user name must start with a letter and can include numbers and _. Otherwise it is illegal. Prompt user with error output "User name contains only alphabetic data and underscores" when user name detection is valid.

3. Determine if the user name already exists , if present , and then determine whether the user has set a password, if set password, direct exit, not set, the password is set to the specified password after the correct output mode display "Username password has been updated and exit"

4. when the user name does not exist , the user is created and the specified password is set for the user to display the "user username created and updated password" in the correct output mode

5. Requires no additional output results that are not required during script execution. The script should be returned in a non-correct way to return the parameter to a value other than 0.

Steps to resolve:

thought:

It seems that the problem is quite complicated. And we have to divide the problem into a number of small problems to solve , I simply put this foot for the above five small modules, and then one to solve it.

    • First module

Requirements : Write a script createuser.sh, the execution syntax of the script must be: createuser.sh-u username-m password, options and parameters can support multiple spaces, but can not be reversed in order. Exit script when the correct option or parameter is not specified, "Createuser.sh-u username-m password" is prompted with an incorrect output

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/C3/wKiom1mFd0HCaQlBAABv7Wq0A28577.png "title=" bvg~ D2c9y{[a64xvel ' Y8vp.png "alt=" Wkiom1mfd0hcaqlbaabv7wq0a28577.png "/>

#判断是否满足有四个参数

If [$#-ne 4];then

#提示用户输入错误

echo "Createuser.sh-u username-m password"

#不满足退出

Exit
#第一个参数是否是-U
elif [$! = "-U"];then
echo "Createuser.sh-u username-m password"
Exit

#第三个参数是否是-M
elif [$! = "-M"];then
echo "Createuser.sh-u username-m password"
Exit
Fi

    • A second module

requirement : The username must start with a letter and can include numbers and _. Otherwise it is illegal. Prompt user with error output "User name contains only alphabetic data and underscores" when user name detection is valid.

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M00/9D/C3/wKioL1mFenDy7sVhAAAMZubbNx4005.png "title=" C) u%} An6@y5gm~lg$ycj9if.png "alt=" Wkiol1mfendy7svhaaamzubbnx4005.png "/>

echo $2|egrep-i "^[a-z]+ ([a-z]*|[ _]*| [0-9]*) {100}$

This sentence satisfies the above requirements but does not prompt the user

^[a-z]+ User name start

([a-z]*|[ _]*| [0-9]*) {100}$ behind can be a letter or underscore or a number of their three to make a whole with {100} to repeat 100 times until the end of the user name (repeated 100 times, I don't think anyone can set the user name to 100-bit one.

    • A third module

Requirements : Determine whether the user name already exists, if present, and then determine whether the user has set a password, if set password, direct exit, not set, the password is set to the specified password after the correct output mode display "Username password has been updated and exit"

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/C3/wKioL1mFfIrzH_O8AAA2TAmYNLg276.png "title=" E3bqdf7 ' X}su@kz5nqnj) ol.png "alt=" Wkiol1mffirzh_o8aaa2tamynlg276.png "/>

(cat/etc/passwd |cut-d:-F 1|egrep ^$2$ &&
Cat/etc/gshadow|grep ^$2:|cut-d:-f2|grep! &&

echo "$4" | passwd--stdin $) &>/dev/null && echo $ "password Updated" &&
Exit

# &>/dev/null is the prompt to execute a successful or failed command to discard the display

# echo "$4" | passwd--stdin $ $4 The fourth parameter (passwd) as a password passed to the second parameter (username)

    • Fourth module

Requirements : When the user name does not exist, the user is created and the specified password is set for the user to display the "user username created and updated password" in the correct output mode

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/9D/C3/wKioL1mFfiXiiEvyAABI3Kz3rMs442.png "title="%x{fd (EE56) [6EOP30] B0kw.png "alt=" Wkiol1mffixiievyaabi3kz3rms442.png "/>

(Echo $2|egrep-i "^[a-z]+ ([a-z]*|[ _]*| [0-9]*) {100}$ "&&
Useradd &&

echo "$4" | passwd--stdin) &>/dev/null &&
echo $ "created and updated password" | | echo "User name contains only alphabetic data and underscores" &&
Exit

The fourth module is built on one of the modules.

    • Fifth module

Requirements : Requires no additional output results that are not required during script execution. The script should be returned in a non-correct way to return the parameter to a value other than 0.

The fifth module is a perfect and complementary to the above four modules

&>/dev/null is to meet no other output results

Exit [] is the error parameter returned by exit

    • Comprehensive


650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/C3/wKioL1mFgV6gFW4jAAEJysco8AI060.png "title=" qz_ Kjcglwsh84aifxt[h%%6.png "alt=" Wkiol1mfgv6gfw4jaaejysco8ai060.png "/>

Code:

#!/bin/bash
# Filename createuser53.sh
# revision:1.1
# DATE:2017/8/5
# author:xin
#------------- -----------------------------
#参数是否满足四个
If [$#-ne 4];then
echo "createuser.sh-u username-m password"
Exit 1
-u
elif ["! ="-U "];then
echo" createuser.sh-u username-m password "
Exit 2
#第三个参数是否是 -M
Elif [$! = "-M"];then
echo "createuser.sh-u username-m password"
Exit 2
Fi
#判断用户是否存在 If there is a password set up
(CAT/ETC/PASSWD |cut-d:-F 1|egrep ^$2$ &&
cat/etc/gshadow|grep ^$2:|cut-d:-f2|grep!  &&< Br>echo "$4" | passwd--stdin $) &>/dev/null && echo "password updated" &&
Exit 4
   #用户不存在满足条件创建用户并设置密 Code
   (Echo $2|egrep-i "^[a-z]+ ([a-z]*|[ _]*| [0-9]*] {100}$ "&&
   useradd &&

echo "$4" | passwd--stdin) &>/dev/null &&
echo $ "created and updated password" | | echo "User name contains only alphabetic data and underscores" &&
Exit 5


650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9D/C3/wKioL1mFgq6C4ulpAAAdMueIhOw260.png "title=" ' E ' @ W1@}84~cygxqrr2a_$x.png "alt=" Wkiol1mfgq6c4ulpaaadmueihow260.png "/>

A user named Ceshi password: 123 was created to test successfully

User creation of Linux scripts

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.