Three ways to invoke another shell script in a shell script

Source: Internet
Author: User



The main following are several ways:


Command explanation
Fork A new child shell executes, and the child shell can inherit environment variables from the parent shell, but the environment variables in the child shell are not brought back to the parent shell.
Exec Executes within the same Shell, butexecthe contents of the parent script after the row are no longer executed.
Source Executing in the same Shell, variables and environment variables declared in the invoked script can be obtained and used in the main script, equivalent to merging two scripts in execution.












First: Fork Features: A sub-PID is generated and can be called repeatedly .



?forkIs the most common, is directly in the scriptpath/to/foo.shto use to invoke



foo.sh this script, such as if it is foo.sh in the current directory, is./foo.sh. When running, terminal will open a new child shell execution script foo.sh, when the child shell executes, the parent shell is still in. When the child shell finishes executing, it returns to the parent shell. The child shell inherits environment variables from the parent shell, but the environment variables in the child shell are not brought back to the parent shell.



1. Enter the editor:


[[Email protected] ~] # Vim liqiang-2.sh 


2. Edit the content call notation:


Echo is called here .  /liqiang.sh


3. Output results


[[Email protected] ~] # ./liqiang-2.sh Call Hollohollo here


The second type: Exec feature: All code after exec call does not execute



?execUnlike thisfork, there is no need to open a new child Shell to execute the called script. The invoked script executes within the same Shell as the parent script. Howeverexec, after invoking a new script, the contents ofexecthe parent script after the row are no longer executed. And that's theexecsourcedifference.



1. Edit the content call notation:


echo is called here
./liqiang.sh
echo here is the exec function call
exec ./liqiang-3.sh
echo here is the source
source liqiang-4.sh
echo is called here
./liqiang.sh
echo here is the exec function call
exce liqiang-3.sh
echo here is the source
source liqiang-4.sh
echo is called here
./liqiang.sh
echo here is the exec function call
exce liqiang-3.sh
echo here is the source
source liqiang-4.sh 


2. Output: The feature is that after the first exec executes, the following is no longer executed.


[[email protected] ~] # ./liqiang-2.sh
Call here
hollo
hollo
Here is the exec function call
See how many calls here
Call it once 


The Third Kind: The Sourcesource feature does not generate a sub-PID is a child process, he will be used to copy the script into the current shell script execution, can be repeatedly called.



? Theforkdifference is not to open a child Shell to execute the called script, but to execute it in the same Shell. Therefore, the variables and environment variables declared in the invoked script can be obtained and used in the main script.



1. Edit the content call notation:


echo is called here
./liqiang.sh
echo here is the exec function call this time commented out
#exec ./liqiang-3.sh
echo here is the source
source liqiang-4.sh
echo is called here
./liqiang.sh
echo here is the exec function call commented out
#exce liqiang-3.sh
echo here is the source
source liqiang-4.sh
echo is called here
./liqiang.sh
echo here is the exec function call
exec liqiang-3.sh
echo here is the source
source liqiang-4.sh 


? 2. Implementation results:


[[email protected] ~] # ./liqiang-2.sh
Call here
hollo
hollo
Here is the exec function call that was commented out this time
Here is the source
See how many calls here
No restrictions
Call here
hollo
hollo
Here is the exec function call being commented out
Here is the source
See how many calls here
No restrictions
Call here
hollo
hollo
Here is not called after the exec function call # 


Attention:



The result of the source mode is that both run in the same process. This is the equivalent of merging two scripts and then running them first.



? Assigning permissions to multiple scripts


chmod a+x liqiang-2.sh liqiang-3.sh liqiang-4.sh




Three ways to invoke another shell script in a shell script


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.