Two Methods for referencing and calling another script file in Shell scripts
This article mainly introduces two methods to reference and call another script file in Shell scripts. This article introduces the syntax of the two methods, and then provides the actual use of instances and some notes, for more information, see
In Java and Python, you can use the import method to call scripts or modules. For example:
The Code is as follows:
>>> Import math
>>> Math. sqrt (4)
2.0
In Shell, how do I call other shell scripts, or variables and functions in other scripts?
Method 1: ../subscript. sh
Method 2: source./subscript. sh
Note:
1. There is a space between two points. Please note that.
2. The two scripts are not in the same directory. Use the absolute path.
3. For simplicity, the first method is usually used.
For example:
The Code is as follows:
Main. sh # main script
Subscripts. sh # subscript, or called script
[Code]
[Code]
### Subscripts. sh script content :###
#! /Bin/bash
String = "Hello, World! \ N"
The Code is as follows:
### Main. sh script content ###
#! /Bin/bash
../Subscripts. sh
Echo-e $ {string}
Exit 0
Output result:
The Code is as follows:
# Chmod + x./main. sh
#./Main. sh
Hello, World!
#
Note:
1. The script to be called does not have the execution permission. The script to be called must have the executable permission.
2. chmod + x./main. sh # Note that there is something here, otherwise the bash script may not be found.