Shell operation and simple programming (4)

xiaoxiao2021-03-06  83

Shell Special Character

Wildcard

Wildcards are used for pattern matching, such as file name matching, pathname search, string lookup, etc. Commonly used wildcards are (*), (?) And character sequences in square brackets []. The user can include these wildcards in the file name as the command parameter, constitute a so-called "mode string" to match mode matching during the execution.

* Represents any strings (length can be inequal), for example: f * matches any string of fleece. However, it should be noted that the dot (.) And the slope (/) in the pathname before the file must be explicitly matched. For example, "*" cannot match .file, and ". *" Can match .file.

? Represents any individual characters.

[] Represents a specified character range, as long as the [] location in the file name is within the [] location specified in [], then this file name matches this mode string. The character range in square brackets can be composed of a direct-given character, or consist of a starting character representing a defined range, a termination character, and an intermediate hyphen (-). For example, F [A- D] is the same as F [ABCD]. The shell will use all the file names that match the mode strings specified in the command line to form the final command, and then execute this command.

Table 3-1 describes the specific meaning of these wildcards.

Table 3-1 Meaning of wildcard

Mold string

Meaninglessness

*

Name of all documents in the current directory

* Text *

The name of the file containing text in the current directory

[AB-DM] *

All the names of the files starting with A, B, C, D, M in the current directory

[AB-DM]?

All the names of all the files starting with A, B, C, D, M and only with a character with a character

/ usr / bin / ??

Directory / USR / BIN Name of all names of the two characters

It is particularly important to note that even characters (-) are only valid in square brackets, indicating the character range, such as being a normal character outside in square brackets. And "*" and "?" Are only wildcards outside the square brackets. If there is in square brackets, they lose the ability of wildcards and become ordinary characters. For example, only one pair of brackets in the pattern - a [*?] ABC are wildcards, "*" and "?" Are all normal characters, so it matches the string that can only be - a * ABC and - a? ABC.

Finally, some issues that need to be careful when using wildcards. Since "*", "?" And [] have a relatively special meaning for the shell, these characters should not appear in the normal file name. Especially in the directory name, don't appear, otherwise shell matches the inconsistency. It is also a point to pay attention to: If there is no file name that matches the specified mode string in the directory, the shell will use this mode string itself as a parameter to pass the relevant command. This may be the reason why a special character appears in the command.

quotation marks

The quotes in the shell are divided into three types: single quotes, double quotes and reverse quotes.

1. apostrophe

The characters from the single quotes appear as ordinary characters. After the special characters are enclosed in single quotes, they will lose their original meaning, but only as a normal character interpretation. E.g:

# String = '$ path'

# echo $ String

#Path

It can be seen that $ maintains its own meaning, as a normal character.

2. Double quotes

The characters from the double quotes, except for $, /, 'and "these characters are still special characters and retain their special features, and the rest are still treated as ordinary characters. For $, it is used later. Variables replace this variable and $; for / for example, it is an escape character, it tells Shell Don't give special processing, just when doing normal characters. You can think of it, in double quotes need in front Plus / only 4 characters $, /, 'and "itself. And ", if it is not added in front, the shell will match it the previous". For example, we assume that the value of PATH is.: / Usr / bin: / bIN, enter the following command:

# String = "$ PATH / / /" / $ PATH "

# echo $ teststring

.: / usr / bin: / bin / "$ PATH

The reader can try the result before the second double quotation is tried.

3. Reverse quotation

The key corresponding to the reverse quotation mark (`) character is generally located in the upper left corner of the keyboard, not to confuse it with single quotation marks ('). The string in the reincarnation is interpreted by the shell as a command line. When execution, the shell first executes the command line, and replaces the entire retrieval (including two reverse numbers) in its standard output result. E.g:

# pwd

/ home / xyz

# String = "Current Directory IS` PWD` "

# echo $ String

Current Directour IS / Home / XYZ

When the shell executes the echo command, first perform the command PWD in the `PWD`, the output result / home / xyz replaces the` PWD. Section, the last output is the entire result.

This function using the reverse number can be commanded, that is, assigns the execution result enclosed in the rejoice to the specified variable. E.g:

# Today = `date`

# echo Today Is $ TODAY

Today Is MON APR 15 16:20:13 CST 1999

Directors can also be used nested. However, it is important to note that the inner layer of the inner layer must be escaped with the reverse slope (/). E.g:

# ABC = `echo the number of users is /` WHO | WC-L / ``

# echo $ ABC

The Number of Users IS 5

Annotate

In SHELL programming or Linux configuration documentation, you are often commented to increase the readability of the program. The comment line is expressed in the front of the front of the shell.

转载请注明原文地址:https://www.9cbs.com/read-106578.html

New Post(0)