Linux---Shell (symbols and regular expressions)

Table of contents

1. Symbols

1.1 Quotes

 1.1.1 Single quotation marks (' ')

1.1.2 Double quotes (" ")

1.1.3 Backticks (` `)

1.2 Wildcards

1.2.1 Wildcard " * "

1.2.2 Wildcard " ? "

1.2.3 Wildcard " [ ] "

1.2.4 Wildcard " [!] "

1.3 Connectors

1.3.1 " ; " Connector

1.3.1 " && " connector

1.3.1 " || " connector

Two, regular expressions

2.1 Metacharacters

2.1.1 Basic regular expression metacharacters

2.1.2 Extended regular expression metacharacters


1. Symbols

        Symbols in the Shell have various functions, such as initiation, wildcards, connectors, etc. With the help of these symbols, users can complete more complex functions

1.1 Quotes

        In the shell, quotation marks are mainly used to convert the meaning of metacharacters. The so-called metacharacters refer to characters that have special processing capabilities in regular expressions, such as $, \, > and other characters. There are three kinds of quotation marks in the shell: single quotation marks , double quotes, backticks

 1.1.1 Single quotation marks (' ')

        Restore the characters in the middle to the literal meaning to realize the function of shielding Shell metacharacters. Note: single quotes must exist in pairs, and a single quote cannot be used alone between two single quotes

Such as: NUM=100 | echo $NUM | echo '$NUM' (define variable NUM, assign and output)

  

1.1.2 Double quotes (" ")

        Double quotes have the function of shielding Shell metacharacters, but they will not shield " $ , \ , `` ", and single quotes can appear individually in double quotes

Such as echo " $NUM " 

1.1.3 Backticks (` `)

        Command substitution is possible, and backticks and double quotes can be used in combination, but they cannot be used in combination with single quotes, because the shielding function of single quotes will block the function of backticks

Such as: echo "today is `date `" (output the current time of the system)

        echo ' today is ` date `'

1.2 Wildcards

        Shell wildcards are generally used for data processing or filename matching

1.2.1 Wildcard " * "

        Wildcard " * " can match multiple strings

Such as: ls -d /etc/sys* (match all files starting with sys string under the /etc/ path)

        -d indicates that only the target directory is processed, and the files in the directory are not recursively processed 

1.2.2 Wildcard " ? "

         The wildcard " ? " can only match a single string at a time, and is usually used in combination with other wildcards

Such as: ls -d /etc/???.conf (match files in the /etc/ directory that start with three characters ending with .conf)

1.2.3 Wildcard " [ ] "

        Matches any character within "[ ]", usually a range

Such as: ls /etc/[ad]*.conf

1.2.4 Wildcard " [!] "

        Matches any element except " [! ] "

Such as: ls /etc/y*[!.conf] (match files in the /etc/ directory that start with y and do not end with .conf]

1.3 Connectors

        The shell provides symbols for linking commands. Using these symbols, multiple instructions can be selectively executed in sequence or command execution results

1.3.1 " ; " Connector

        Will let the commands using ";" be executed in sequence

如:SUM=100; echo "$SUM" ; echo "sum is $SUM"

 The execution sequence is executed in sequence, mostly used for installation commands

1.3.1 " && " connector

        The command after the symbol can only be executed if the execution order of the preceding and following commands satisfies the logical AND relationship, and the following command will be executed only after the execution before the "&&" symbol is successful

Such as: cat ./test.txt && echo "the directory exists" (output the test.txt file, if it exists, output "the directory exists")

1.3.1 " || " connector

        Contrary to the "&&" connector, but the following command will be executed only when the command before the symbol fails to execute

Such as: cat ./test.txt || echo "the directory exists" (output the test.txt file, if it does not exist, output "the directory does not exist")

Two, regular expressions

        Regular expressions are a set of predefined rules (also known as patterns), which are usually used in text search and replacement

Related concepts:

Regular expressions can filter text because it defines a series of metacharacters. The combination of metacharacters and other characters forms certain rules. Only text that meets the rules can be retained, otherwise it will be filtered

2.1 Metacharacters

2.1.1 Basic regular expression metacharacters

        By default, it is performed under the grep command, which conforms to the basic regular expression

(1) Qualifier " * "

        Used to match leading characters 0 or more times

Such as: ls hel*o (list the files that start with hel and end with o)

 

(2) Dot character " . "

Use to match any single character         except the newline character "\n"

Such as: ls he. (list the files starting with he and ending with any [except "\n"] characters)

(3) The beginning of the line locator " ^ "

        Used to match the first character of the line, indicating that the beginning of the line is the character after "^"

Such as: ls /etc | grep "^sys" (list the files starting with sys in the etc directory)

 

(4) End of line locator " $ "

        Characters used to match the last line of text, indicating the character at the end of the line , opposite to the beginning of the line

Such as: ls /ect | grep conf$ (list the files ending with conf in the /etc directory)

 

(5) Character group "[ ] "

        Used to specify a set of characters

Such as: ls ./one | grep test[1-9] (list the files in the /one directory that start with test and end with numbers 1-9)

 

(6) Exclusive character group "[^]"

        Indicates that the characters in [] are not matched, and the usage is opposite to that of the character group [^]

2.1.2 Extended regular expression metacharacters

        Extended regular expressions use extended regular expressions by default. If you want to use grep, you need to use the -E option

(1) Qualifier " + "

        The qualifier "+" is similar to the symbol "*" and can match its leading character multiple times, but the "*" sign supports 0 matches, while the qualifier "+" matches at least once

Such as: ls hel*o (list the files that start with hel and end with o)

       ls egrep hel+o

 

(2) Qualifier " ? "

        Limit the leading character to appear at most once, that is, the leading character can appear 0 or 1 time

Such as ls /etc | egrep "sss?" (list the files beginning with sss in the etc directory)

 

(3) " | " and "()" symbols 

        The " | " symbol is used for the "or" operation between regular expressions, and the "()" symbol is a set of optional sets. The two combinations are usually used to represent a set of optional sets

ls /etc | egrep "(ssh | ssl | ^yum)" (list the files beginning with ssh or ssl or yum in the ect directory

Guess you like

Origin blog.csdn.net/weixin_64428129/article/details/127405658