Call another script in shell script

Source: Internet
Author: User

Call another script in shell script

Bash shell commands are divided into two types: External commands and Internal commands. External commands are implemented through system calls or independent programs, such as SED and awk. Internal commands are implemented by special file formats (. Def), such as CD, history, and exec.

Fork (/directory/script. Sh)

First, any program we use is a child process generated by the parent process, will return to the parent itinerary. The current image is fork in Linux.

When a child itinerary is born, it will allocate a certain amount of resources from the parent itinerary, and (more importantly) Accept the parent itinerary.

* The actual environment changes are those that will be sent to the sub-itinerary.
To put it simply, "regionality" is the criterion for determining local and local changes.
However, from the perspective of environment variables, we do not expect another important feature of environmental changes:
* The change in the environment can only be committed from the parent itinerary to the Child itinerary. In other words, how to change the environment in a sub-itinerary does not affect the environment of the parent itinerary.

Fork is the most common. It uses/directory/script. Sh directly in the script 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 are not brought back to parent-shell. The unique difference between a child process and a parent process lies in the PID (process ID ).

Exec (exec/directory/script. Sh)

Exec also allows the script to upload rows on the same itinerary, but the original itinerary is ended.

That is, when the original itinerary is stopped, it is the maximum difference between exec and source/fork.

When the exec command is executed, the current shell process is closed, and the subsequent command is switched to continue execution. (If the original thread is killed, create a new thread ??? Right ?)

Source (Source/directory/script. Sh)

* The written source allows the script to write lines in the shell before the execution, rather than generating a sub-shell.

The results of all the preceding rows are completed in the shell. If the script environment changes, it also changes to the previous environment ﹗

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.

 

You can use the following two scripts to understand the differences between the three call methods:

1. Sh
#! /Bin/bash
A = B
Echo "PID for 1.sh before exec/source/fork: $"
Export
Echo "1.sh:\ $ A is $"
Case $1 in
Exec)
Echo "using exec ..."
Exec./2.sh ;;
Source)
Echo "using source ..."
../2.sh ;;
*)
Echo "using fork by default ..."
./2.sh ;;
Esac
Echo "PID for 1.sh after exec/source/fork: $"
Echo "1.sh:\ $ A is $"

2. Sh
#! /Bin/bash
Echo "PID for 2.sh:$ $"
Echo "2.sh get \ $ A = $ A from 1.sh"
A = C
Export
Echo "2.sh:\ $ A is $"

Run the following three scripts to observe the results:

1. $./1.sh fork
2. $./1.sh Source
3. $./1.sh Exec

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.