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

What are the linux shell custom function methods?

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

Share

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

This article introduces the relevant knowledge of "what are the methods of linux shell custom functions?". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Define shell function (define function)

Syntax:

[function] funname [()]

{

Action

[return int;]

}

Description:

1. You can define it with function fun () or directly fun () without any parameters.

2. If the parameter is returned, you can display the plus: return return. If you do not add it, the result will be run with the last command as the return value. Return followed by the value n (0-255)

Instance (testfun1.sh):

#! / bin/sh fSum 3 2; function fSum () {echo $1 command not found 2; return $(($1 recording 2));} fSum 5 7; total=$ (fSum 3 2); echo $total,$?; sh testfun1.sh testfun1.sh: line 3: fSum: command not found 5 Lines 7 3

From the above example, we can draw several conclusions:

1. The function must be declared before the function is called. The shell script is run line by line. It will not be precompiled like other languages. At one time, you must declare a function before using it.

2. Total=$ (fSum 3 2); through this calling method, we know clearly that inside the single parentheses in shell, it can be: command statement. Therefore, we can think of a function in shell as defining a new command, which is a command, so the input parameters are directly separated by spaces. Once, the parameter method in the command can be obtained by: $0. Get $n. $0 represents the function itself.

3. The function returns a value that can only be returned through $? The system variable gets, directly through =, gets a null value. In fact, according to the above understanding, we know that a function is a command, and it is necessary to use $? to get the return value of the command in shell. Get.

Second, the function scope, the variable scope of action

First, let's look at an example (testfun2.sh):

#! / bin/sh echo $(uname); declare num=1000; uname () {echo "test!"; ((num++)); return 100;} testvar () {local num=10; ((num++)); echo $num;} uname; echo $? Echo $num; testvar; echo $num; sh testfun2.sh Linux test! 1001 11 1001

Let's analyze the above example and come to the following conclusion:

1, the definition function can be the same as the system command, indicating that the shell search command, first of all in the current shell file defined in the place to find, find direct execution.

2. Need to get the function value: through $? Get

3. If you need to send out other type function values, you can define the variable (this is the global variable) before the function call. It can be modified directly inside the function, and then the modified value can be read out when the function is executed.

4. If you need to define your own variable, you can define it in the function: local variable = value, then the variable is an internal variable, and its modification will not affect the value of the same variable outside the function.

These are the summaries of my experience in using linux and shell functions in my work. Have you mentioned any places? welcome to communicate!

The following are the supplements of other netizens:

All the scripts so far in this tutorial have been executed from beginning to end. This is good, but you may have noticed that some of the script segments repeat each other.

Shell allows you to form a set of command sets or statements into a single available block called the shell function.

The definition format of a function in shell is as follows:

Function name () {

Command1

Command2

...

CommandN

[return value]

}

If you prefer, you can prefix the function name with the keyword function, depending on the user.

Function function name () {

Command1

Command2

...

CommandN

[return value]

}

The function returns the value, which can display the increment return statement; if not, the result of the last command run is taken as the return value (usually 0, and the error code is returned if execution fails). The return is followed by a numeric value (0-255).

Functions can be placed in the same file as a piece of code, or in a separate file that contains only functions. Functions do not have to contain many statements or commands, and can even contain only one echo statement, depending on the consumer.

The following example defines a function and calls it:

#! / bin/bashdemoFun () {echo "This is your first shell function!"} echo "Function begin..." helloecho "Function end!" Output: Function begin...This is your first shell functionalized function end!

The following defines a function with a return statement:

#! / bin/bashfunWithReturn () {echo "The function is to get the sum of two numbers..." echo-n "Input first number:" read aNumecho-n "Input another number:" read anotherNumecho "The two numbers are $aNum and $anotherNum!" return $(($aNum+$anotherNum))} funWithReturnecho "The sum of two numbers is $?!" The output looks like this: The function is to get the sum of two numbers...Input first number: 25Input another number: 50The two numbers are 25 and 50! The sum of two numbers is 75!

The return value of the function passes $? after the function is called. To get.

Note: all functions must be defined before use. This means that the function must be placed at the beginning of the script until it is first discovered by the shell interpreter. The calling function can only use its function name.

This is the end of the content of "what are the methods of linux shell custom functions?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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