I. shell script parent-child Process Analysis
Code:
#!/bin/bash echo $$ ps -ef |grep `echo $$` echo "-------------------------------------" ( pwd;echo $$;ps -ef |grep `echo $$`;) echo "-------------------------------------" { pwd;echo $$;ps -ef |grep `echo $$`; }
Result 1:
Result Analysis 1:
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, which 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 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 used directly to execute the script line by line.
Result 2:
Result Analysis 2
The running result is the same as running 1, bash cpro. sh and run cpro directly. the working mechanism of sh is the same, and an execution shell will be derived to execute the script row by row;
Summary:
Executing Internal commands in the current shell will not dispatch a child shell, so some internal commands can change the current shell execution environment;
Ø when executing external commands or scripts in the current shell, the execution of the commands will not affect the current shell environment;
If you want to execute the script in the current shell without distributing the child shell, you can use the. And 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 execute the script directly in the current shell. The execution of the script affects the current shell environment.
2. Pipeline and shell script execution relationship command | read var and comand | while read var
Code:
pwd | read var echo $var #1 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. When bash executes pwd | read var, the sub-shell is executed on both sides of the pipeline, and echo $ var is executed on the parent shell. The parent shell cannot read the sub-shell variable.
Bash executes ls-l | while read var. The left side is the sub-shell, and the right side is the parent shell, so the last part can be read.
Summary of shell script execution:
1. built-in commands (builtin)
It is built-in by shell interpreter. It is directly executed by shell and does not need to derive new processes. Some internal commands can be used to change the current shell environment, such:
Cd/path
Var = value
Read var
Export var
...
2. External commands ("externalcommand" or "disk command ")
The binary executable file needs to be loaded into memory by the disk 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)
The shell interpreter fork + exec executes this script command. In the exec call, the kernel checks 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.
3. How to check whether a command is built-in or an external command (Program)
After you enter the command, shell is generally fork and runs 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.
$ Type echo echoisa shell builtin ·-t parameter, return 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-a will display the command PATH.
Other related commands:
The which command can only find executable programs in path, that is, external programs. The built-in commands cannot be identified.
File command to view the file type. file will try to read the file header and parse the file type