Shell Script implementation of the Detailed

When Shell script runs, it first looks for system environment variables ENV, the environment variable specifies a file (usually /etc/profile,~/.bash_profile,~/.bashrc,/etc/bashrc load order, etc.), loading after the above-mentioned environment variable file, Shell began performing content Shell script.

Shell scripts are top-down, command execution and statements of each line from left to right, that is, over the implementation of a command and then execute a, if you encounter sub-script Shell script (ie nested script), the content will be the first implementation of the sub-script, return to the parent after the completion of the script to continue execution of subsequent commands and statements within the parent script.

Normally, when Shell script execution, the kernel will start requesting a new process to execute commands and scripts Shell scripts in this sub-process, the basic process is shown:
Shell Script implementation of the Detailed

Shell scripts are usually executed in several ways can be used.
1) bash script-name or sh script-name: It is when there is no executable permissions (i.e., file permission bits x script file property itself - No need to use) the method often used, or the beginning of the script files are specified interpreter method;
2) path / name or script-./script-name: refers in the current path to execute the script (script requires execute permissions), the required permission to the script file to the executable (i.e., x plus attributes file permission bits ), specific methods chmod + x script-name. Then an absolute or relative path can be performed by script scripts directly;
. 3) Script-source name or .script-name: "." This method is commonly used source or (dot) is read or loaded designated Shell Script file (such as san.sh), then followed by the implementation of all the statements specified in the Shell script file san.sh. These statements of the parent Shell script father.sh process run in the current (several other new models will start the process of implementation of the sub-script). Thus, use or source. "" San.sh itself may be a script or function values of the variables such as the current parent returns the value passed to the script father.sh Shell used.
4) sh <script-name or cat scripts-name | sh: The same applies to the bash, but this usage is not very common, but sometimes it can also have the effect of a surprise move, such as: do not loop to achieve the boot from the start to streamline services case is by all spliced form of the command string, and then via duct to bash operation;

Interested friends can try it yourself!

I personally try to come to the conclusion there are three points:
1. Sub Shell script directly inherited from the parent Shell script variables, function (like a son with his father named, also inherited his father's genes), etc., not conversely;
2. If you want to turn, inherit (as if their father with his son surname, so that gene also inherited his father's son), we must advance with the source or load the child in the parent Shell scripts Shell scripts ".";
3. by source or " . "load scripts have been executed since the implementation of the script in the current Shell, so after the end of the script, the script variables (including function) value still exists in the current Shell, and sh and bash execute scripts will start a new child Shell execution, after the implementation return to the parent Shell. Therefore, variables (including function) values, etc. can not be reserved. Shell scripts during the development, if there is demand for a reference or execute the scripts or configuration files of other scripts, the best use. "" Or source to load the script or configuration file, after processing, then load them into the following script, you can call the contents of the script and configuration file variables and functions such as loading the source.

Guess you like

Origin blog.51cto.com/14157628/2432456