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

Introduction and use of shell function

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Note: the following content is drawn from the shell programming practice document published by the old boy.

What is a function?

In the shell script, the function is similar to the command alias in the Linux system, which can simplify the code amount of the program and make the program

Easier to read, easier to change, easier to use.

To put it simply, the function of a function is to combine the same code that has been called many times in the program (function body) and give it a name (that is, the function name). All other places that want to call this part of the code repeatedly need to call this name. When you need to modify this part of the repeated code, you only need to change a part of the code in the function body to modify all calls, or you can write the function into a file independently, and then load it into use when you need to call the function.

The advantages of using the shell function are as follows:

Defining the same program segment as a function can reduce the code amount of the whole program, improve the development efficiency, increase the readability and readability of the program, improve the management efficiency, and realize the functional modularization of the program. make the program universal (portable). Second, the syntax of shell function

(1) the standard is written as follows:

This format is recommended for the function function name () {#. What is written in the curly braces is the command to be executed return n}

(2) the simplified writing is as follows:

The function name () {# does not use the function method instruction. Return n}

In the syntax of the shell function, function means to declare a function, which can be omitted.

Third, the execution of shell function

The functions of shell are divided into the most basic functions and the functions that can pass parameters, which are executed as follows:

(1) when executing a function without parameters, you can enter the function name directly (note that there are no parentheses). The format is as follows:

Function name

Important notes on executing functions:

When executing a shell function, do not take the function before the function name and the parentheses after the function; the definition of the function must be defined or loaded before the program to be executed; the execution order of various programs in the shell execution system is: system alias → function → system command → executable command; when the function executes, it shares variables with the script that calls it, and can also set local variables and special location parameters for the function. In the shell function, the function of the return command is similar to that of exit. The function of return is to exit the function, while exit is the exit script; the return statement returns an exit value (that is, the return value) to the current program calling the function, while exit returns an exit value (that is, the return value) to the current shell; of the executing program. If you store the function in a separate file, you need to use source or "." when it is loaded by the script. To load. Local is generally used to define local variables within a function, and these variables disappear after leaving the function.

(2) the function execution method with parameters is in the following format:

Function name parameter 1 parameter 2

The function is followed by a description of the parameters:

Location parameters of shell ($1, $2..., $#, $? And $@) can be used as parameters of the function; at this time, the parameters of the parent script are temporarily masked or hidden by the parameters of the function; $0 is special, it is still the name of the parent script; when the function execution is complete, the parameters of the original command line script can be restored; the parameter variables of the function are defined in the function body. 4. Examples of basic use of shell function

(1) example 1:

[root@localhost ~] # vim a.shanzhizbinba # the following is the definition of an and b two functions function a () {echo "aaaaaaaaaaa"} function b () {echo "bbbbbbbbbbb"} b # execute the function name in the script in order to call the function, here first call the function ba # and then call the function a [root@localhost ~] # sh a.sh#, the execution result is as follows

The old boy wrote about the content of the shell function, only two big scripts have not been written, it is recommended to look at the original document to understand shell, the latter content, I do not have much patience to write, can be at the end of my blog top article, download the original document to learn.

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

Wechat

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

12
Report