Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to reuse external Shell scripts

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to reuse external Shell scripts", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to reuse external Shell scripts" this article.

The way external scripts are executed

If there is an a.sh script in the current directory, the content is as follows

#! / bin/bash echo "a.sh..."

There are several main ways to execute external scripts in a script

Source external script name

The b.sh script in the current directory contains the following:

#! / bin/bash source a.sh echo "b.sh."

Execute. / b.sh, and the result is as follows

[root@ecs-centos-7] #. / b.sh a.sh. B.sh...

The source a.sh command in the script first executes the a.sh script in the current directory, so the result will output a.sh first. Then output the print of the b.sh script itself

Dot external script name

Change the statement in the b.sh script that executes the a.sh script to a period + space + a.sh. The modified script is as follows:

Note: a space must be added between the period and a.sh, otherwise there will be an error during execution.

#! / bin/bash. A.sh echo "b.sh."

Execute. / b.sh, and the result is as follows

[root@ecs-centos-7] #. / b.sh a.sh. B.sh...

In the above script,. A.sh will execute the a.sh script first, and the result will output a.sh first. And then output b.sh.

Sh external script name

Sh external script name and. / external script name are the same, and it doesn't matter which one you choose. Here is an example of the previous method.

Change source a.sh in the b.sh script to sh a.sh, and the modified script is as follows:

#! / bin/bash sh a.sh echo "b.sh."

Execute the. / b.sh command, and the result is as follows

[root@ecs-centos-7] #. / b.sh a.sh. B.sh...

As you can see, the result output is the same as the above two ways.

What's the difference between the three ways?

There are three ways to call external scripts: source external script, dot external script, and sh external script. What's the difference between them?

Among them, source external script and dot external script are the same. The current script inherits the global variables and functions of the external script, which is equivalent to importing the functions and global variables of the external script into the current script.

Modify the a.sh and b.sh scripts as follows

A.sh script

#! / bin/bash VAR_A=10 func_a () {echo "a.sh.pidpura parallel parampura 1"}

B.sh script

#! / bin/bash source a.sh func_a $1 echo "vara:$VAR_A" echo "b.sh.pidpura $"

Execute the. / b.sh 5 command, and the result is as follows

[root@ecs-centos-7] #. / b.sh 5 a.sh.. pidvara:10 b.sh...pid:21485 21485 parampura 5 vara:10 b.sh...pid:21485

The $$in the two scripts refers to the process ID that executes the script. As can be seen from the results, both a.sh and b.sh are executed in the same process, so executing the source a.sh command in the b.sh script will import the global variable VAR_A and the function func_a from the a.sh script into b.sh.

Print the variable VAR_A in b.sh, and the output value is the same as that in a.sh. Call the func_a function, and the output also shows that the function in a.sh is called.

Source external script and dot external script are the same, so modify the source a.sh in b.sh to. A.sh, execute. / b.sh 5, the result is still the same

Because sh external scripts are executed in two different processes, the current script cannot directly use functions and global variables in the external script

Modify the a.sh and b.sh scripts as follows

A.sh script

#! / bin/bash test_a () {echo "a.sh...test_a"} echo "a.sh.pidazzi $"

B.sh script

#! / bin/bash sh a.sh echo "b.sh.pidbank $" test_a

Execute the. / b.sh command, and the result is as follows

[root@ecs-centos-7 ~] #. / b.sh a.sh...pid:21818 b.sh...pid:21817. / b.sh: line 7: test_a: command not found

As can be seen from the results, the processes ID that execute a.sh and b.sh are different, and the b.sh script process cannot find the test_a function, so calling the test_a function in b.sh will prompt that the command cannot be found.

Call a function in an external script

The way we talked about sh external scripts in the previous section is that functions and global variables in external scripts cannot be used directly. Here are several ways to solve this problem.

Case branch selection

This method is similar to the switch case statement in the program code, which selects different branches through switch to execute different logic. The shell script is implemented using the case keyword.

A.sh script

#! / bin/bash VAR_A=10 test_a () {echo "test_a..pid:$$,p1:$1,p2:$2"} get_var () {echo ${VAR_A}} case "$1" in ta) test_a $2 $3;; var) get_var;; *) echo "parameter err..." Esac

B.sh script

#! / bin/bash echo "b.sh.pidgroup" $"sh a.sh ta 3 5 ret=$ (sh a.sh var) echo" ret:$ret "

Execute the. / b.sh command, and the result is as follows

[root@ecs-centos-7 ~] #. / b.sh b.sh...pid:24813 test_a..pid:24814,p1:3,p2:5 ret:10

The script b.sh starts by printing the process ID that calls itself.

The sh a.sh ta 3 5 statement calls the a.sh script. The three parameters passed in are ta, 3, 5. When a.sh is executed, the first parameter ta is matched by case, and the test_a function is called, and the remaining two parameters 3 and 5 are passed into the function as parameters.

The a.sh script is called in the ret=$ (sh a.sh var) statement, passing in a var parameter, and the get_var function is called after case matching. The function of this function outputs the value of the global variable VAR_A in the script, and the function of $() in the statement is to get the return value of the command in (). Here, the return value of the get_var function in the a.sh script is assigned to the ret variable. So the value of this variable is the value of the global variable VAR_A in the a.sh script

Note: if you want to get the return value of the function, you can print the corresponding output value with echo in the function, and then use $(function name argument list) to get the printed value in the function, such as the ret=$ (sh a.sh var) statement in the b.sh script above. The value of the variable ret is the value output by the get_var function in the a.sh script.

It is important to note that if there is an echo debug log in the function, the debug log will also be returned.

Function call template

There is a disadvantage of using the case keyword to match different functions called above. Every time a function is added to an a.sh script, case needs to add a branch to call different functions. You also need to pay attention to whether the function has parameters passed in and whether the number of parameters is correct.

We can solve the above problem by adding the following statement to the end of each script for external invocation, as follows

If [$#-ge 1]; then name= "$1" shift 1$ name "$@" fi

The above statement first determines the number of parameters passed in when the script is called. It is valid only if the number of parameters is greater than or equal to 1. The first parameter passed in represents the function name, and will be passed into the function as a parameter from the second parameter to the last parameter.

Here, shift 1 moves the parameters of the incoming script to the left. For example, the parameters of the incoming script have three parameters: $1 $2 $3. After moving one position to the left, $2 moves to the position of $1, $3 moves to the position of $2, and the number of parameters becomes 2.

Reason: among the parameters passed into the script, the first parameter is the function name, and the second parameter is the function parameter. If you do not move to the left, the first parameter function name will also be passed into the function as an argument.

Here is the complete script

A.sh script

#! / bin/bash VAR_A=10 test_a () {echo "test_a..pid:$$,p1:$1,p2:$2"} get_var () {echo ${VAR_A}} if [$#-ge 1]; then name= "$1" shift 1$ name "$@" fi

B.sh script

#! / bin/bash echo "b.sh.pidgroup ret=$ $" sh a.sh test_a 3 5 ret=$ (sh a.sh get_var)

Execute the. / b.sh command, and the result is as follows

[root@ecs-centos-7 ~] #. / b.sh b.sh...pid:25086 test_a..pid:25087,p1:3,p2:5 ret:10

As you can see, the result is the same as the case method above.

Now other scripts can call the functions in the a.sh script through the sh a.sh function name argument list, and get the return value of the a.sh script function by the way of $(sh a.sh function name argument list).

Advantages and disadvantages of both

Compared with the method selected by the case branch, the advantage of the function call template is that the caller only needs to care about the function name, the function input parameters and the function return value in the reused script.

The disadvantage is that if multiple scripts call the function in the reuse script, when the function name is changed in the reuse script, you need to modify all the places where it is called.

The disadvantage of the function call template method is precisely the point of the case branch selection method. When the case branch is selected, different functions are called according to the passed string parameters, where the string parameter is equivalent to the alias of the function. As long as this parameter remains unchanged, the function name in the script can be changed at will.

The above comparison of advantages and disadvantages is only a relative comparison, which is not obvious in practical application. In most cases, both methods can be used.

The above is all the contents of the article "how to reuse external Shell scripts". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report