Developed on the Linux platform, another script file is often executed on the console. The following methods are frequently used:./My. ShOrSource my. ShOr. My. Sh; What are the differences between the three methods? Let's take a look at how to call another shell script in a shell script. The methods include:Fork exec source.
1. Fork (/directory/script. Sh ):
If the shell contains the execution command, the sub-command does not affect the parent-level command. After the sub-command is executed, the sub-command is executed again. Child-level environment variables do not affect the parent-level.
Fork is the most common, that is, directly using/directory/script in the script. sh to call the script. sh script. run a sub-shell script to execute the call. When Sub-shell is executed, parent-shell is still running.
After sub-shell is executed, the system returns parent-shell. Sub-shell to inherit environment variables from parent-shell. However, the environment variables in sub-shell will not be brought back to parent-shell.
2. Exec (exec/directory/script. Sh ):
After a sub-level command is executed, the parent-level command is no longer executed.
Unlike fork, exec does not require a new sub-shell to execute the called script. The called script and the parent script are executed in the same shell. However, after exec is used to call a new script, the content after the exec line in the parent script will not be executed again. This is the difference between exec and source.
3. Source (Source/directory/script. Sh ):
After executing the sub-level command, continue to execute the parent-level command. At the same time, the environment variables set for the sub-level will affect the environment variables of the parent level.
The difference with fork is that instead of opening a sub-shell to execute the called script, it is executed in the same shell. therefore, the variables and environment variables declared in the called script can be obtained and used in the main script.
The above three methods are different for calling shell scripts ,. /My. SH is the fork method, source my. SH and. my. SH is both a source method.
In Linux, to build an embedded development platform, you must execute scripts to set environment variables before cross-compiling code. Remember to execute shell scripts in sourc or vertex mode. The reason is as follows.