Shell basics 67: execution sequence (| and &, {} and () and regular table expressions

Source: Internet
Author: User
From: http://bbs.chinaunix.net/viewthread.php? Tid = 445229

Quote: $ unset
$ [-N "$ A"] & ["$ A"-lt 100] | echo 'too big! '
Too big!

Why can we get the above results? Mark

Question 10
10) & | what is the difference?
Http://bbs.chinaunix.net/forum/viewtopic.php? T = 218853 & show_type = new & postdays = 0 & postorder = ASC & START = 60

Question 6:
6) What is the difference between exec and source?
Http://www.chinaunix.net/forum/viewtopic.php? T = 194191

Summary:
Fork: how to change the environment in the sub-itinerary does not affect the environment of the parent itinerary.

Normally, when we execute a shell script, we first generate a sub-Shell Sub-itinerary, and then the sub-shell will generate the sub-itinerary of the command line.
That is, when we run a script normally:

[Copy to clipboard] [-]

Code:

./My. Script

Source: the so-called source is to allow the script to be executed in the current shell, rather than generating a sub-shell for execution.
Because all execution results are completed in the current shell, if the script environment changes, of course, it will change the current environment.

[Copy to clipboard] [-]

Code:

Source./My. Script
Or:
../My. Script

() And {}

Quote: If you want to combine several commands for execution, s h e l provides two methods. You can execute a group of commands either in the current s h e l or in the child s h e l.
1 ,():
To execute a group of commands in the current s h e l, you can use the command separator to separate each command and enclose all the commands in parentheses.
It generally takes the following form:

[Copy to clipboard] [-]

Code:

(Command 1; command 2 ;...)

2 ,{}:
Use {} to replace (), then the corresponding command will be executed as a whole in the sub-s h e l, only when the output of all commands in {} is redirected as a whole, the commands are placed in the sub-s h e l for execution, otherwise, it is executed in the current s h e l. It generally takes the following form:

[Copy to clipboard] [-]

Code:

{Command 1; command 2 ;...}

This section is incorrect. The following is now available:

[Copy to clipboard] [-]

Code:

(Cmd1; cmd2;...; cmdn) # execute a group of commands in a sub-shell
{Cmd1; cmd2;...; cmdn} # execute a group of commands in the Current Shell
This is a basic concept

[Jason @ firewall Jason] $ A = 1; echo $ A; {A = 2 ;}; echo $
1
2
[Jason @ firewall Jason] $ A = 1; echo $ A; (a = 2;); echo $
1
1

{A = 2;} changed the current shell variable value.
(A = 2;) The current shell variable value is not changed

You can view the original link
Http://bbs.chinaunix.net/forum/viewtopic.php? Show_type = & P = 3386007 #3386007
Thank you for pointing out.

Example 1: In the above example:

[Copy to clipboard] [-]

Code:

$ Comet month_end.txt | exit

If the script fails to be executed, I want to send an email to myself first and then exit. You can use the following method to implement this:

[Copy to clipboard] [-]

Code:

$ Comet month_end | (echo "Comet did no work" | mail Sam; Exit)

In the above example, because only command delimiters are used and they are not combined, s h e l will directly execute the last command (e x I t ).

Example 2: The original example is as follows:

[Copy to clipboard] [-]

Code:

$ Sort quarter_end.txt> quarter. Sorted & LP quarter. Sorted

Use the command combination method. If the s o r t command is successfully executed, first copy the output file to a log area and then print it.

[Copy to clipboard] [-]

Code:

$ Sort quarter_end.txt> quarter. Sorted & (CP quarter. Sorted/logs/quarter. Sorted; LP quarter. Sorted)

Shell 13th question 7:
7) What is the difference between () and?
Http://bbs.chinaunix.net/forum/viewtopic.php? T = 218853 & show_type = new & postdays = 0 & postorder = ASC & START = 45

At the beginning of the RegEx below, there are not many posts in this part, and there are also a lot of posts in the Forum.

In the future, we will post the regular expression rules in the Forum:
Regular expression syntax
Http://bbs.chinaunix.net/forum/viewtopic.php? T = 149723

Post an old article about regular expressions, which is really helpful for beginners.
Http://www.chinaunix.net/forum/viewtopic.php? T = 63273

[Post] examples of regular expressions that are frequently used
Http://www.chinaunix.net/forum/viewtopic.php? T = 190716

My common Regular Expressions
Http://bbs.chinaunix.net/forum/viewtopic.php? T = 190088

Regular Expression expression (For details, refer to the experts)
Http://bbs.chinaunix.net/forum/viewtopic.php? T = 247322

Regular Expression fog, regular expression syntax comparison among various tools
Http://bbs.chinaunix.net/forum/viewtopic.php? T = 425827

[Original] On the greedy nature of Regular Expressions
Http://www.chinaunix.net/forum/viewtopic.php? T = 15825

[Copy to clipboard] [-]

Code:

"Match the beginning and end of a row.
"Match the dataset.
"Only matches letters and numbers.
"Matches string sets within a certain range.

When extracting or filtering text from a file or command output, you can use a regular expression (r e), which is a set of special or not special string modes.

Basic metacharacters and their meanings

Quote: The original post is published by "net player:
ABC: three character characters of ABC, but they are independent from each other rather than a set. (three Char. set can be written in a simple form)
(ABC): a set of three character characters ABC. (The unit can be regarded as a char. Set)
A | B: a single character, or A or B.
(ABC | XYZ): represents one of the two Char. Sets, ABC or XYZ. (Limit 2)
[ABC]: A single character, which can be A, B, or C. (same as the [ABC] principle of wildcard)
[^ ABC]: A single character, Not A, B, or C. (with wildcard [! ABC] Same principle)
.: Represents any single character. (Which of the following is wildcard? Same principle)

[Copy to clipboard] [-]

Code:

^ Match only the beginning of a row
$ Only matches the end of a row
* Only one single character followed by *, matching 0 or more single characters
[] Only matches the characters in. It can be a single character or a character sequence. Available-
Indicates the range of character sequences in []. For example, [1-5] is used to replace [1 2 3 4 5].
/Is only used to block the special meaning of a metacharacter. Because sometimes some metacharacters in s h e l contain
Special meaning. /Can make it meaningless
. Only match any single character
P a t e r n/{n/} is only used to match the occurrence times of the preceding p a t e r n. N is the number of times
P a t e r n/{n,/} m only means the same as above, but the minimum number of times is n
P a t e r n/{n, m/} only indicates the same as above, but the occurrence times of p a t e r n are between N and M.
The special meanings are described in detail.

1. Use a period to match a single character

Example 1: Beng. N: starts with B e g and contains any character in the middle.
Example 2:... x C...: a total of 10 characters. The first four are followed by XC.
Example 3: List directories or files with write permissions for all users:

[Copy to clipboard] [-]

Code:

Ls-L | grep... x. x.. x

2. Match the string or Character Sequence with ^ at the beginning of the line
^ Only matching characters or words at the beginning of a line are allowed.

For example, use the LS-l command and match the directory.

[Copy to clipboard] [-]

Code:

$ LS-L | grep ^ d

3. match strings or characters at the end of a row with $
It can be said that $ is the opposite of ^. It matches strings or characters at the end of a row, and $ is placed after matching words.

Example 1: List all rows ending with the word common in the file httpd1.conf.

[Copy to clipboard] [-]

Code:

$ Grep common $ httpd1.conf
Or
$ Cat httpd1.conf | grep common $

Example 2: match all empty rows: ^ $

Example 3: Only one character row is returned: ^. $

4. Use/to block the meaning of a special character

The following characters are generally considered special characters:

[Copy to clipboard] [-]

Code:

$. '"* [] ^ | ()/+?

Quote: for example:
/.
The character after the backslash is no longer a special character, but a common character, that is, a period.

Quote: If you want to match the rows that contain ^, put the backslash before it to block its special meaning:
/^

Quote: in a regular expression, match all objects ending with *. p a s:
/*/. P A S
To block the specific meaning of the character.

5. the number of times that the/{/} match results appear
Use * to match all matching results at any time, but if you only need to specify the number of times, use /{/},

Quote: This mode has three forms:
Pattern/{n/} match mode appears n times.
Pattern/{n,/} match mode appears at least N times.
Pattern/{n, m} match mode appears between N to m times, n, m is 0-2 5 any integer in 5.

Example 1: match letter A appears twice and end with B:

[Copy to clipboard] [-]

Code:

A/{2/} B

The matching value is a B.

Example 2: Match A at least 4 times:

[Copy to clipboard] [-]

Code:

A/{4,/} B

The result is a B or a A a B, but not a B.

Example 3: If the range of occurrences is given, for example, a appears twice to 4 times:

[Copy to clipboard] [-]

Code:

A/{2, 4/} B

The result is a B, A a B, A a B, rather than a B or a A a B.

Example 4: extract code from the following list:

Quote: 1234xc9088
4523xx9001
0011xa9912
9931xc3445

The format is as follows: the first 4 characters are numbers, followed by X, and the last 4 characters are numbers. The operation is as follows:
[0-9]/{4/} X [0-9]/{4 /}

Quote: the meanings are as follows:
1) The matching number appears four times.
2) followed by code X.
3) the number appears four times.

The result is as follows:

Quote: 1234xc9088-No match
4523xx9001-match
0011xa9912-No match
9931xc3445-No match

Examples of Regular Expressions

[Copy to clipboard] [-]

Code:

^ First line
$ End of line
^ [T h e] For rows starting with t h e
[S] I g n a [L] for matching words s I g n a l, s I g N A L
[Ss] igna [ll]/. Same as above, but add a sentence
[M a y m a Y] a row containing uppercase or lowercase letters of m a y
^ U s e r $ rows that only contain U S E R
[Tty] $ pairs of rows ending with T Y
/. For rows with periods
^ D. x directory that has executable permissions for users, user groups, and other user group members
^ [^ L] list of directories for excluding associated Directories
[. * 0] add any character before or after 0
[0 0 0 *] 0 0 0 or more
[II] uppercase or lowercase I
[I] [n] uppercase or lowercase I or N
[^ $] Empty rows
[^. * $] Match any string in the row
^ ...... $ A row containing 6 Characters
[A-Za-Z] For any single character
[A-Z] [A-Z] * at least one lowercase letter
[^ 0-9/$] identifies a non-digit or dollar
[^ 0-0 A-z A-Z] for non-numbers or letters
[1 2 3] a number between 1 and 3
[D] e v I c e for words d e v I c e or d e v I c e
The first two letters of d e. c e are d e, followed by two arbitrary characters, and finally c e.
/^ The Q pair starts with ^ Q.
^. $ Rows with only one character
^/. [0-9] [0-9] indicates the row starting with a period and two numbers.
'"D e v I c e"' indicates the word d e v I c e.
D e [V v] I c e/. For words d e v I c e or d e v I c e
[0-9]/{2/}-[0-9]/{2/}-[0-9]/{4/} pair Date Format D-m -Y
[0-9]/{3 /}/. [0-9]/{3 /}/. [0-9]/{3 /}/. [0-9]/{3/} on the I p address format NNN. nnn. nnn. nnn
[^. * $] Match any row

Related Article

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.