Linux invoke/reference/Include script file method in Shell

Source: Internet
Author: User

Script A (test example 1)

The code is as follows Copy Code
#!/bin/bash
Echo ' Your are in the '

Q) Call another script file in the current script file?

Method One: Using source

Script second (test example 2)

The code is as follows Copy Code
#!/bin/bash
Echo ' Your are in second file '
SOURCE A

Method Two: use.

The code is as follows Copy Code
Script second (test example 3)
#!/bin/bash
Echo ' Your are in second file '
. The


SOURCE filename and. FileName should be the same thing, both executing scripts in the * current *shell environment. You can also use the SH filename, which executes the script in the child shell of the current shell.

You can use the following two feet to realize the difference between three ways of calling:

The code is as follows Copy Code

1.sh

#!/bin/bash
A=b
echo "PID for 1.sh before exec/source/fork:$$"
Export A
echo "1.sh: $A is $A"
Case is 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 $A"

2.sh

#!/bin/bash
echo "PID for 2.sh: $$"
echo "2.sh get $A = $A from 1.sh"
A=c
Export A
echo "2.sh: $A is $A"

Implementation status:

$./1.sh
PID for 1.sh before exec/source/fork:5845364
1.sh: $A are B
using fork by default ...
PID for 2.sh:5242940
2.sh get $A =b from 1.sh
2.sh: $A are C
PID for 1.sh after exec/source/fork:5845364 1.sh: $A is b
$./1.sh exec
PID to 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 are 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 are C
PID for 1.sh after exec/source/fork:5156894 1.sh: $A is C
$

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.