Post:
Http://blog.chinaunix.net/u1/43550/showart_372639.html
1. General wildcard characters
Wildcard characters are used for pattern matching, such as file name matching, path name searching, and string searching. There are four common wildcards:
* Match any character 0 or multiple times. For example, f * can match any string with the F header. Note that the dot (.) in front of the file name must be explicitly matched with the slash (/) in the path name.
? Match any character, for example, F? Matches F1, fa, and Fb, but does not match F, fabc, and F12.
[] There is a character group that matches any character specified by the character group. This character group can be composed of directly given characters, and can also be composed of starting characters, ending characters, and a hyphen (-) that represents a limited range. For example, F [a-d] and f [ABCD] serve the same purpose.
! Represents characters not listed in square brackets. For example, F [! 1-9]. C indicates to start with F. The name of the. c file after a character is not from Number 1 to 9. It matches fa. C, FB. C, and FM. C.
In a regular expression, you can use both "*" and "?".
2. Expression
A pattern expression is a word that contains one or more wildcard characters. Bash not only supports General wildcards, but also provides a unique extension pattern matching expression. Its form and meaning are as follows:
(1) * (mode table) matches the "mode" of the given mode Table 0 times or multiple times, and the modes are separated by "|. For example, file *(. c |. o) match the file and file. c. File. o, file. c. c, file.0.0, file. c. o, file. o. c, but does not match file. h or file. s.
(2) + (mode table) matches the "mode" of a given mode table once or multiple times. The modes are separated by "|. For example, file + (. c |. O) matches file. C, file. O, file. c. O, and file. C, but does not match file.
(3 )? (Schema table) the matching schema table appears 0 times or once in any of the "Patterns". The modes are separated by "|. For example, file? (. C |. o) only file and file are matched. C and file.0, which do not match multiple or repeated modes, that is, do not match file. c. c. File. c. 0.
(4) @ (schema table) matches only one occurrence of a pattern given in the schema table, and each pattern is separated by "|. For example, file @ (. c |. 0) matches file. C and file.0, but does not match file, file. C. C, file. c. O, and so on.
(5 )! (Schema table) in addition to a "schema" in a given schema table, it can match anything else.
We can see that the definition of a pattern expression is recursive, and each expression can contain one or more patterns. For example, file * (. [CHO] |. Sh) is a valid pattern expression. However, it should be noted that because expressions with "*" and "+" can match the combination of the given pattern, it is dangerous to delete the file by using this expression, the system configuration file may be deleted by mistake. Therefore, you must be careful when using it.
3. quotation marks
In shell, there are three types of quotation marks: single quotation marks, double quotation marks, and inverted quotation marks.
(1) double quotation marks
Characters enclosed by double quotation marks, except for the special functions of $, inverted quotation marks ('), and backslash ("), all other characters are treated as common characters. "$" Indicates the replacement of variables, that is, replacing $ with the value of the variable specified later. The inverted quotation marks indicate the replacement of commands; only when "" is followed by the following characters, "" is the Escape Character. These characters are: "$", "'", "", "", or line break. The Escape Character tells shell not to perform special processing on the character after it, just as a common character. For example:
$ Echo "my current DIR is 'pwd' and LOGNAME is $ LOGNAME"
My current DIR is/home/ABC and LOGNAME is ABC
(2) single quotes
All characters enclosed by single quotes are common characters. For example,
$ Echo 'the time is 'date', the file is $ home/abc'
The time is 'date', the file is $ home/ABC
(3) Inverted quotation marks
The string enclosed in inverted quotation marks is interpreted by shell as a command line. During execution, shell will first execute this command line and replace the entire inverted quotation mark with its standard output result. I have seen it in the previous example. For example,
$ Echo current directory is 'pwd'
Current directory is/home/ABC
4. Notes
The body line starting with "#" in the shell program represents the comment. For example:
#! /Bin/bash
# If no arguments, then listing the current directory.
# Otherwise, listing each subdirectory.
If test $ # = 0
Then ls ·
Else
For I
Do
Ls-L $ I | grep '^ d'
Done
Fi
The above program consists of the IF statement, where the else part is the for loop statement. This function is used to detect the number of location parameters. If it is equal to 0, the current directory itself is listed. Otherwise, the subdirectories contained in each location parameter are displayed.
In the above Code, the first line #! /Bin/bash indicates that the following script is written in bash and must be interpreted and executed by the bash program. The next two rows start with "#", indicating that this is a comment line. The comment line can be used to describe the functions, structures, algorithms, and variables of a program and increase the readability of the program. The shell ignores the comment line during execution.