Two Methods for referencing and calling another script file in Shell scripts

Source: Internet
Author: User

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.

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.