2 ways to reference and invoke another script file in a shell script _linux shell

Source: Internet
Author: User
Tags chmod

In Java,python, you can use the import method to make calls between scripts or modules, for example:

Copy Code code as follows:

>>> Import Math
>>> Math.sqrt (4)
2.0

How do you invoke other shell scripts in the shell, or variables in other scripts, functions?

Method one:. ./subscript.sh
Method Two: source./subscript.sh

Attention:

1. Between two points, there are spaces, you must pay attention to.
2. Two scripts are not in the same directory, use absolute path
3. For the sake of simplicity, the first method is usually used

For example:

Copy Code code as follows:

Main.sh #主脚本
Subscripts.sh #子脚本, or the script that was tuned
[Code]
[Code]
# # #subscripts. sh script contents are as follows: ###

#!/bin/bash
String= "hello,world! \ n "

Copy Code code as follows:

# # #main. sh script content as follows ###

#!/bin/bash
. ./subscripts.sh
Echo-e ${string}
Exit 0

Output results:
Copy Code code as follows:

# chmod +x./main.sh
#./main.sh
hello,world!
#

Attention:

1. The scripted script can not execute permissions, the calling script must have executable permissions
2.chmod +x./main.sh #注意这里要有点, or bash scripts may not be found

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.