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 script under Linux

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to reuse external shell scripts under Linux, which is very detailed and has a certain reference value. Interested friends must read it!

In Linux development, shell scripts are often written to perform some tasks, usually a script only does one thing. As the task increases, there will be more scripts and more reusable places. At this time, you need to extract the common functions of the script and put it into a general script, and other scripts can reuse it.

In Linux development, shell scripts are often written to perform some tasks, usually a script does only one thing. As the task increases, there will be more scripts and more reusable places. At this time, you need to extract the common functions of the script into a general script, and other scripts can reuse it.

This article describes how to execute external scripts in shell scripts, how to call functions in external scripts, and how to reuse related methods in scripts.

The way external scripts are executed

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

#! / bin/bashecho "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/bashsource a.shecho "b.sh."

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

[root@ecs-centos-7] #. / b.sha.sh.b.shh.

The source a.sh command in the script will first execute 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.shecho "b.sh."

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

[root@ecs-centos-7] #. / b.sha.sh.b.shh.

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/bashsh a.shecho "b.sh."

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

[root@ecs-centos-7] #. / b.sha.sh.b.shh.

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

What is the difference between the three ways to call external scripts there are source external scripts, dot external scripts, sh external scripts, what is 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/bashVAR_A=10func_a () {echo "a.sh.pidpura parallel parampura 1"}

B.sh script

#! / bin/bashsource a.shfunc_a $1echo "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 5a.sh.pidpur21485paramVaraVera 10b.sh.pidVera 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/bashtest_a () {echo "a.sh...test_a"} echo "a.sh.pidazzi $"

B.sh script

#! / bin/bashsh a.shecho "b.sh.pidbank $" test_a

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

[root@ecs-centos-7 ~] #. / b.sha.sh...pid:21818b.sh...pid:21817

. / b.sh: line 7: test_a: command not found. From the result, we can see that the process ID that executes a.sh and b.sh is different. 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 was not 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/bashVAR_A=10test_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/bashecho "b.sh.pidgroup ret:$ret $" sh a.sh ta 3 5retting $(sh a.sh var) echo "ret:$ret"

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

[root@ecs-centos-7] #. / b.shb.sh.pidVera 24813 testocrata... pidRose 24814 parcels p1RO 3Len p2RZ 5retril 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.

When the ret= statement calls the script, a parameter is passed in, and the function is called after matching. The function of this function outputs the value of the global variable 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 the 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 the function name parameter list to get the printed value in the function, such as the (sh a.sh var) statement in the script above. The value of the variable ret is the value output of 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, there are 2 and 3 parameters in the script. After moving one position to the left, 2 moves to the position, 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/bashVAR_A=10test_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/bashecho "b.sh.pidbank $" sh a.sh test_a 3 5retting $(sh a.sh get_var)

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

[root@ecs-centos-7] #. / b.shb.sh.pidpur25086testoria25087Participationp1pur3Linep2pur5retly10

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 under Linux". Thank you for reading! Hope to share the content to help you, more related 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

Development

Wechat

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

12
Report