Shell fourth (top)

Source: Internet
Author: User

Fourth article
Understanding shell command Execution Sequence analysis

Each line that the Shell reads from a standard input or script is called a pipe (pipeline); it contains one or more commands, separated by one or more pipe characters (|).

In fact, there are many special symbols that can be used to separate individual commands: semicolons (;), Pipes (|), &, Logical AND (&&), and Logical OR (| | )。 For each read pipeline, the shell will split the command back, set the I/O for the pipeline, and perform the following operations on each command in turn:


The entire sequence of steps, as shown, looks somewhat complex.
When the command line is processed, each step is taken in the Shell's memory; The shell doesn't really show you what each step is going to do.
So, you can assume that we're peeping through the shell's memory and know how the command line at each stage is being transformed.
Let's start with that example:

  



The above-mentioned implementation steps are summarized as follows:

1. The command begins with the shell syntax and splits it into tokens. The most important point is that I/O redirection >out is identified here and stored for later use. The process continues with the following line, where the range of each token is displayed on the line below the command:

Echo ~+/${f}[12] $y $ (echo cmd subst) $ ((3 + 2))
| 1 | |------2------| | 3 | |---------4--------| |----5-----|

2. Check if the first word (echo) is a keyword, such as if or for. This is not the case, so the command line continues processing.
3. Check if the first word (echo) is an alias. Not here. So the command line does not change, continue processing.
4. Scan so that the word needs to be expanded with a wavy number. In this example, ~+ is an extension of ksh93 and bash, which is equivalent to $pwd, which is the current directory. Token 2 will be modified to handle the following:

ECHO/TMP/X/${F}[12] $y $ (echo cmd subst) $ ((3 + 2))
| 1 | |-------2-------| |3 | |--------4----------| |----5-----|

5. The next step is the variable expansion: Token 2 and 3 are both modified. This will produce:

ECHO/TMP/X/${F}[12] A B $ (echo cmd subst) $ ((3 + 2))
| 1 | |-------2-------| | 3 | |--------4----------| |----5-----|

6. Again to deal with is the command substitution. Note that you can recursively apply all the steps in the list! Here, the command substitution modified Token 4:

ECHO/TMP/X/${F}[12] A b cmd subst $ ((3 + 2))
| 1 | |-------2-------| | 3 | |---4----| |----5-----|

7. Now perform arithmetic substitution. The change is token 5, the result:

ECHO/TMP/X/${F}[12] A b cmd subst 5
| 1 | |--------2--------| | 3 | |---4----| |5|

8. All the results from the previous deployment will be scanned again to see if there are any $IFS characters. If there are, then they are as delimiters (separator), resulting in extra words, for example, two characters $y is originally composed of a word, a single expanded "A-space-B", at this stage is cut into two words: A and B. The same way is also applied to the result of the command $ (echo cmd subst). The previous token 3 became token 3 with
Token 4. The previous token 4 was the token 5 and token 6. Results:

ECHO/TMP/X/${F}[12] A b cmd subst 5
| 1 | |--------2--------| 3 4 |-5-| |-6-| 7 |

9. The final replacement phase is a wildcard expansion. Token 2 becomes token 2 with token 3:

echo/tmp/x/$f 1/tmp/x/$f 2 a b cmd subst 5
| 1 | |-----2-----| |----3----| 4 5 |-6-| |-7-| 8 |

10. At this point, the shell is ready to execute the final command. It will go looking for Echo. Just ksh93 and Bash's echo are built into the shell.

11.Shell actually executes the command. I/O redirection of > Out is performed first, then the internal echo version is called, and the last parameter is displayed.

The final result:

$cat out/tmp/x/f1/tmp/x/f2 a b cmd subst 5

  

Refer to Linux commune: http://www.jb51.net/article/34326.htm

Shell fourth (top)

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.