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
$
|