17. Create a user's grep command and a powerful $

Source: Internet
Author: User


???? Officially write


    1. Demand simulation

For example, one day we made a very good software.

For security purposes, the software is not recommended to run with the root account at runtime. So we'll have an init shell script to create the user and initialization file


1, to determine whether the user exists (for example, God), did not create

2. Set password to 123 after creation

3, to copy some program files in the past, and in addition to root, can only God user run (preferably root is not easy to run)



    1. grep command searches for files and contents in files

Powerful Text Search tool that can use regular expressions to search for text and print matching lines.

This is also one of the most used commands for routine maintenance and system deployment.

???? 1. Format

grep "The string you want to retrieve" file name

grep "The string you want to retrieve" file name *

grep ' regular ' file name

Cat file name | grep "You like the string"

ls | grep "You like the filename"

???? 2. Common parameters

???? -C: Outputs only the count of matching rows.

-I: Case insensitive (only for single-character).

-H: The file name is not displayed when querying multiple files.

-L: Only file names that contain matching characters are output when querying multiple files.

-N: Displays matching lines and line numbers.


Example: CAT/ETC/PASSWD | grep ' ^pangzi '-C

    1. stdin non-interactive input analog keyboard

Using passwd to change passwords requires prompt and confirmation if we force setup OK?

echo "123" | passwd User name--stdin

stdin standard input (keyboard)

--stdin represents the use of non-interactive mode input, the previous statement is a pipeline

  1. shell1

    USER _count= ' cat/etc/passwd | grep ' ^god: '-C '

    user_name= ' God '

    if [$USER _count-ne 1]


    ???? Useradd $USER _name

    ???? echo "123" | passwd $USER _name--stdin


    P style= "text-align:justify" >???? echo ' OK '



    Cons: users cannot create and modify passwords repeatedly

  2. Powerful markers (partial)

    $ #传递到脚本的参数个数

    $$ the current process ID number for the script to run

    $! Process ID Number of the last process running in the background

    [Email protected] and $ #相同, but use quotation marks and return each parameter in quotation marks

    $? Displays the exit status of the last command. 0 means no error, and any other value indicates an error (or unsuccessful execution)



  3. shell2 optimized repeatable execution

    User_count= ' cat/etc/passwd | grep ' ^god: '-C '

    User_name= ' God '


    Useradd $USER _name

    If [$?-eq 0] #判断上一个命令的执行状态, 0 is a failure


    echo "123" | passwd $USER _name--stdin

    echo "Done"


    echo "Error"




17. Create a user's grep command and a powerful $

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.