Learning bash notes-Command Line Processing

Source: Internet
Author: User
Tags builtin

Each line read by shell from a standard input or script is called a pipeline line. It contains one or more commands separated by 0 or multiple pipeline operators, each of which is read by shell.

To perform the following operations.

 
1. The command is divided into space, tab, newline,;, (,), <,>, |, and &. Mark type
Includes words, keywords, I/O redirections, and semicolons.
2. Check the first mark of each command and view it as a keyword without quotation marks or backlash. If it is an open keyword, such as if and
Other control structures start with a string, function, {or (, the command is actually a compound command. Shell internally performs composite commands
Process, read the next command, and repeat this process. If the keyword is not the starting string of the composite command (for example, it is a control
Keywords that appear in the middle of the structure, such as then, else, or do), shell gives a syntax error signal.
3. Check the first keyword of each command based on the alias list. If a match is found, replace the alias definition and return to the first step;
Otherwise, go to step 1. This policy allows recursive aliases and defining keyword aliases.
4. Execute braces extension. For example, a {B, c} is changed to AB AC.
5. If ~ It is located at the beginning of the word and replaced with the user's main directory.
6. Execute parameter replacement for any expression starting with $.
7. Replace the expression in the form of $ (string) with a command.
8. evaluate an arithmetic expression in the form of $ (string.
9. Divide the line parameters, commands, and arithmetic replacement parts into words again. This time, it uses the characters in $ ifs as delimiters instead of the elements in step 1.
Character Set.
10. For *,? And.
11. Search for other source code and use the first word as the command. First, it is a function command, then a built-in command, and then
$ Path.
12. Run this command after setting I/O redirection and other operations.
This step list is very direct, but not all of it. There are several ways to modify the process: Use reference, use command, builtin, or enable,
And use the advanced command eval.
 
1. Reference 
The reference can be seen as a way for shell to ignore certain steps in the above 12 steps. In particular:
The first 10 steps are bypassed by single quotes.
Double quotation marks bypass steps 1 to 5, steps 9 and 10. That is, the pipe characters, aliases, and ,~ Replacement, wildcard extension, and
Splits a word into separators. The single quotation marks in double quotation marks also do not apply. Double quotation marks can be included in double quotation marks by adding a backslash to the front.
 
2. Command, builtin, and enable 
Command does not search for aliases, keywords, and functions. Only commands in Built-in commands and paths found in all paths are executed.
For example:
cd()
{
#some process
command cd
}
Avoid the function falling into a recursive loop. Place the command in front of the CD to ensure that the built-in command CD is executed, rather than the function is called.
 
Builtin is similar to command, but more strict. It only looks for built-in commands. Builtin can also be used in the above example.
 
Enable is used to block a built-in command.
Use enable-a to display all commands (available and unavailable)
Use enable to display all available built-in commands. Enable command can be used to make a command available.
Enable-N can be used to display all blocked built-in commands. Enable-N command can be used to shield a command.
For example:
[[Email protected] ~] # Enable
enable .enable :enable [
[Omitted]
Enable CD
[Omitted]
[[Email protected] ~] # Enable-N CD
[[Email protected] ~] # Enable-n
Enable-N CD
[[Email protected] ~] # Enable-aenable. Enable: Enable [
[Omitted]
Enable-N CD
[Omitted]
[[Email protected] ~] # Cd/dev
-Bash: CD: Command not found
[[Email protected] ~] # Enable CD
[[Email protected] ~] # Cd/dev/
[[Email protected] Dev] #
3. The evaleval statement notifies shell to receive eval parameters and run them again through all the steps processed by the command line. For example, the following script. SH: var1 = "ls | less" $ var1 :#. /. shls: |: no such file or directoryls: less: no such file or directory. The reason is that, following the steps in the preceding command, step 1 is executed: execute the parameter replacement for any expression starting with the symbol $, and then continue to perform the following steps. The pipeline character in the Command has never been resolved, and is finally executed as the LS parameter, cause error. The correct script should be: var1 = "ls | less" eval $ var1 because: 
The first step is to replace the parameters of any expression starting with $, continue to the following steps, and finally determine when running the command
Is eval. Return to the first step to continue the execution. At this time, the pipeline operator is parsed and the final command is successfully executed.
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.