Linuxshell script file execution analysis

Source: Internet
Author: User
Tags builtin
Linuxshell script file execution analysis 1. shell script parent-child Process Analysis code: [cpp] #! /Bin/bashecho $ ps-ef | grep 'echo $ 'echo & quot; --------------------------------------- & quot; (pwd; echo $; ps-ef | grep 'e...
Linux shell script file execution analysis 1. shell script parent-child Process Analysis code: [cpp] #! /Bin/bash echo $ ps-ef | grep 'echo $ 'echo "-------------------------------------" (pwd; echo $; ps-ef | grep 'echo $ ';) echo "-----------------------------------" {pwd; echo $; ps-ef | grep 'echo $ ';} Result 1: www.2cto.com
Result analysis: 1. the last line shows that the ID of the currently logged on shell process is 9561; 2. the third line shows that a shell execution process with a process ID of 10767 is derived from the login shell. The process is responsible for scanning the shell script for execution. 3. 10767 the script execution process is responsible for executing the script line by line: When a built-in command is run directly in the 10767 process, and when an executable program is run, a new sub-process fork is executed. 4. processes with process numbers 10768 and 10769 are child processes derived from the execution of shell 10767 processes ps and grep. 5. 10767 when the script execution process scans the (com1; com2;) statement Group, another command and statement with the ID of 10771 sub-shell will be sent to execute (): 10771 line-by-line scan () statement in, according to the rules in Step 3: Process number 10773 grep and another process ps are 10771 and derived sub-processes, the parent process of the ps sub-process is 10771, which is not shown here. 6. 10767 when the script execution process scans the {com1; com2;} statement, no new child shell will be sent, and the current process 10767 will be directly used to execute the script line by line. Result 2:

Result 2: The running result is the same as that of running 1, bash cpro. sh and run cpro directly. the working mechanism of sh is the same, and an execution shell will be derived and executed in a row-by-row read script; www.2cto.com conclusion: Ø executing internal commands in the current shell will not send a child shell, therefore, some internal commands can change the current shell execution environment. Ø when executing external commands or scripts in the current shell, the execution of commands will not affect the current shell environment; you can run the script in the current shell instead of the child shell. and the source Command + script name for execution. /command. sh = bash. /command. sh The current shell will send a child shell to execute the script file, and the script execution will not affect the current shell environment .. /command. sh = source. /command. sh executes the script directly in the current shell, and the execution of the script is affected. To the current shell environment II. command | read var and comand | while read var code: [cpp] pwd | read var echo $ var #1 www.2cto.com ls-l | while read var do echo $ var #2 done Result analysis: #1 output location #2 output value: this is the secret of the pipeline. bash executes pwd | read var on both sides of the pipeline through a sub-shell, execute echo $ var is executed by the parent shell, and the parent shell cannot read the subshell variable bash and execute ls-l | while read var, the left side of the subshell is executed by the parent shell, so we can finally read the summary of shell script execution: 1. built-In Commands (builtin) are built-in by shell interpreter, which are directly executed by shell and do not need to derive new processes. some internal commands Command can be used to change the current shell environment, such as: cd/pathvar = valueread varexport var... www.2cto.com 2. the binary executable file of an external command ("externalcommand" or "disk command") needs to be loaded into the memory for execution. A new process is derived. the shell interpreter calls a copy of fork itself and then uses the exec series function to execute external commands. then, the external command replaces the subshell of the previous fork. 3. shell script (script) the shell interpreter fork + exec will execute this script command. in the exec call, the kernel will check the first line of the script (for example :#! /Bin/sh), find the interpreter used to execute the script, and then load the interpreter to explain and execute the script program. There may be many kinds of interpreter programs, various shell (Bourne shell, Korn shell cshell, rc and its variants ash, dash, bash, zshell, pdksh, tcsh, es ...), awk, tcl/tk, regular CT, perl, python, and so on. The program is obviously a sub-process of the current shell. If this interpreter is the same shell as the current shell, such as bash, it is the subshell of the current shell, and all the commands in the script are executed in the subshell environment, does not affect the current shell environment. Www.2cto.com 3. how to check whether the built-in command is an external command (program) after the user inputs the command, shell generally fork and execute the command in the sub-shell. This is not the case with built-in commands. Executing a built-in command is equivalent to calling a function in a Shell process and does not create a new process. How can I check whether the command is a built-in command? Type command type-write a description of command type. If no parameter is added, whether the command is a built-in command or an external command is displayed. [Cpp] $ type echo echoisa shell builtin ·-t parameter, returned value file: indicates an external command, generally an external executable program, ELF format alias: indicates the name set by the command alias. builtin indicates that the command is a built-in command function of bash; $ type-t ls alias $ type-t alias builtin $ type-t chmod file www.2cto.com ·-a will display the command PATH. Other related commands: the which command can only find the executable program in path, that is, the external program. The built-in command cannot identify the file command to view the file type. the file will try to read the file header and parse the file type. http://blog.csdn.net/hittata/article/details/8017368
 
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.