When the Shell script runs, it will first look for the system environment variable ENV, which specifies the environment file (the order of loading is usually /etc/profile, ~/.bash_profile, ~/.bashrc, /etc/bashrc, etc.) , After loading the above environment variable file, Shell starts to execute the content in the Shell script.
Shell scripts execute each line of commands and statements from top to bottom and from left to right, that is, execute one command before executing the next one. If encountered in the Shell script When a sub-script (ie script is nested), the content of the sub-script will be executed first, and then return to the parent script to continue executing the subsequent commands and statements in the parent script.
Usually, when a Shell script is executed, a new process will be requested from the system kernel to execute the script’s commands and sub-Shell scripts in the process. The basic process is shown in the figure:
The execution of Shell script can usually be adopted The following ways:
1) bash script-name or sh script-name
This is when the script file itself does not have executable permissions (that is, the file permission attribute x The bit is -) the method often used, or the method that needs to be used when the interpreter is not specified at the beginning of the script file;
For example:
[[emailprotected] ~]# vim test.sh
echo'this is a shell scripts!'
[[emailprotected] ~]# sh test.sh
this is a shell scripts!
[[emailprotected] ] ~]# bash test.sh
this is a shell scripts!
2) path/script-name or ./script-name
refers to the current path To execute the script (the script needs to have execution permission), the permission of the script file needs to be changed to executable (that is, the file permission attribute plus x bit), the specific method is chmod+x script-name. Then you can directly execute the script through the script’s absolute path or relative path;
For example:
[[emailprotected] ~]# ./test.sh
- bash: ./test.sh: Insufficient permissions
[[emailprotected] ~]# chmod u+x test.sh
[[emailprotected] ~]# ./test.sh
this is a shell scripts!
This method is more troublesome! Every time you finish writing a script, you must give the script a permission to execute, otherwise it will prompt an error of “insufficient permissions”.
3) source script-name or .script-name
This method usually uses source or “.” (dot) to read or load the specified Shell script file ( Such as san.sh), and then execute all the statements in the specified shell script file san.sh in turn. These statements will run in the current parent Shell script father.sh process (the other modes will start a new process to execute the child script). Therefore, using source or “.” can pass the variable value or the return value of the function in the san.sh script to the current parent shell script father.sh for use.
For example:
[[email protected] ~]# chmod ux test.sh
[[email protected] ~]# ll test.sh
-rw-r--r--. 1 root root 32 August 26 03:09 test.sh
[[emailprotected] ~]#. test.sh
this is a shell scripts!
[[email protected] ~]# source test.sh
this is a shell scripts!
4) sh
The same applies to bash, but this usage is not very common, but sometimes it can have surprisingly winning effects, for example: the case of simplifying the startup service without looping statements is to use all strings Concatenated into the form of commands, and then passed to bash through the pipeline;
For example:
[[email protected] ~]# ll test.sh
-rw- r--r--. 1 root root 32 August 26 03:09 test.sh
[[email protected] ~]# sh this is a shell scripts!
[[email Protected] ~]# cat test.sh|bash
this is a shell scripts!
A simple example to understand the particularity of the third execution method:
[[email Protected] ~]# echo'user=`whoami`'> test2.sh
[[email Protected] ~]# cat test2.sh
user=`whoami`
[[emailprotected] ~]# sh test2.sh
[[emailprotected] ~]# echo $user
[[emailprotected] ~]#. test2.sh< br />[[e mail protected] ~]# echo $user
root
I personally tried to draw three conclusions:
1. The child shell script will directly inherit the parent Shell script variables, functions (as if sons follow the father’s surname, genes will inherit the father’s), etc., and vice versa; 2. If you want to inherit the other way around (as if letting the father follow the son’s surname, let the father’s genes Also inherited from the son), it is necessary to use source or “.” to load the child shell script in advance in the parent shell script; 3. Load the executed script through source or “.”, because the script is executed in the current shell, so in After the script ends, the values of variables (including functions) in the script still exist in the current Shell, and the sh and bash execution scripts will start the execution of a new child Shell, and return to the parent Shell after execution. Therefore, the values of variables (including functions) cannot be retained. During Shell script development, if there is a need to reference or execute other scripts or configuration files in the script, it is best to use “.” or source to load the script or configuration file first, and then load them to Below the script, you can call the variables and functions in the script loaded by the source and the configuration file.
The same applies to bash, but this usage is not very common, but sometimes it can have surprisingly winning effects, for example: the case of simplifying the startup service without looping statements is to use all strings Concatenated into the form of commands, and then passed to bash through the pipeline;
For example:
[[email protected] ~]# ll test.sh
-rw- r--r--. 1 root root 32 August 26 03:09 test.sh
[[email protected] ~]# shthis is a shell scripts!
[[email Protected] ~]# cat test.sh|bash
this is a shell scripts!
A simple example to understand the particularity of the third execution method:
[[email Protected] ~]# echo'user=`whoami`'> test2.sh
[[email Protected] ~]# cat test2.sh
user=`whoami`
[[emailprotected] ~]# sh test2.sh
[[emailprotected] ~]# echo $user
[[emailprotected] ~]#. test2.sh< br />[[e mail protected] ~]# echo $user
root
I personally tried to draw three conclusions:
1. The child shell script will directly inherit the parent Shell script variables, functions (as if sons follow the father’s surname, genes will inherit the father’s), etc., and vice versa; 2. If you want to inherit the other way around (as if letting the father follow the son’s surname, let the father’s genes Also inherited from the son), it is necessary to use source or “.” to load the child shell script in advance in the parent shell script; 3. Load the executed script through source or “.”, because the script is executed in the current shell, so in After the script ends, the values of variables (including functions) in the script still exist in the current Shell, and the sh and bash execution scripts will start the execution of a new child Shell, and return to the parent Shell after execution. Therefore, the values of variables (including functions) cannot be retained. During Shell script development, if there is a need to reference or execute other scripts or configuration files in the script, it is best to use “.” or source to load the script or configuration file first, and then load them to Below the script, you can call the variables and functions in the script loaded by the source and the configuration file.