Three different ways to invoke another script in a shell script (fork, exec, source)--Reprint

Source: Internet
Author: User



Original link: http://blog.chinaunix.net/uid-22548820-id-3181798.html


    • Fork (/directory/script.sh): If the shell contains execution commands, the subcommands do not affect the parent's commands. When the subcommand executes and then executes the parent command, the child's environment variable does not affect the parent .


Fork is the most common, is directly in the script with/directory/script.sh to call script.sh this script. Run the time to open a Sub-shell execute the call script, Sub-shell execution, Parent-shell is still in.



Sub-shell returns Parent-shell when execution is complete. Sub-shell inherit environment variables from Parent-shell. But environment variables in Sub-shell do not bring back Parent-shell


    • exec (exec/directory/script.sh): The parent command is no longer executed after the command of the child is executed .


Unlike fork, Exec does not need to open a new Sub-shell to execute the called script. The invoked script executes within the same shell as the parent script. However, after calling a new script with exec, the content after the Exec line in the parent script is no longer executed. This is the difference between exec and source.


    • Source (source/directory/script.sh): executes the child command and resumes execution of the parent command, while the environment variables set by the child affect the environment variables of the parent.


The difference from fork is that it does not open a new Sub-shell to execute the called script, but rather executes it in the same shell. So the variables and environment variables declared in the invoked script can be obtained and used in the main script.



The following two scripts can be used to realize the difference of three kinds of calling methods:



1.sh


 
 
 1 #!/bin/bash
 2 A=B 
 3 echo "PID for 1.sh before exec/source/fork:$$"
 4 export A
 5 echo "1.sh: \$A is $A"
 6 case $1 in
 7         exec)
 8             echo "using exec…"
 9             exec ./2.sh ;
10         source)
11             echo "using source…"
12             . ./2.sh ;
13         *)
14             echo "using fork by default…"
15             ./2.sh ;
16 esac
17 echo "PID for 1.sh after exec/source/fork:$$"
18 echo "1.sh: \$A is $A"                        


2.sh


 
1 #!/bin/bash
2 echo "PID for 2.sh: $$"
3 echo "2.sh get \$A=$A from 1.sh"
4 A=C
5 export A
6 echo "2.sh: \$A is $A"


Status of implementation:


$ ./1.sh 
PID for 1.sh before exec/source/fork:5845364
1.sh: $A is B
using fork by default…
PID for 2.sh: 5242940
2.sh get $A=B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:5845364
1.sh: $A is B
$ ./1.sh exec
PID for 1.sh before exec/source/fork:5562668
1.sh: $A is B
using exec…
PID for 2.sh: 5562668
2.sh get $A=B from 1.sh
2.sh: $A is C
$ ./1.sh source 
PID for 1.sh before exec/source/fork:5156894
1.sh: $A is B
using source…
PID for 2.sh: 5156894
2.sh get $A=B from 1.sh
2.sh: $A is C
PID for 1.sh after exec/source/fork:5156894
1.sh: $A is C
$





Three different ways to invoke another script in a shell script (fork, exec, source)--Reprint


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.