Shell Learning 21 - Shell Functions

Functions allow us to divide a complex function into several modules, making the program structure clearer and the code reuse rate higher. Like other programming languages, Shell also supports functions. Shell functions must be defined before use.
The definition format of the Shell function is as follows:
function_name () {
list of commands
[ return value ]
}
You can also prefix the function name with the keyword function if you wish:
function function_name () {
list of commands
[ return value ]
}
For the return value of the function, you can explicitly add a return statement; if not, the result of the last command will be used as the return value.
The return value of the Shell function can only be an integer, which is generally used to indicate whether the function is executed successfully or not. 0 indicates success, and other values ​​indicate failure. If you return other data, such as a string, you will often get an error message: "numeric argument required".
If you must let the function return a string, you can first define a variable to receive the calculation result of the function, and the script accesses this variable to obtain the function return value when needed.
Let's look at an example first:
#!/bin/bash
# Define your function here
Hello () {
echo "Url is http://see.xidian.edu.cn/cpp/shell/"
}
# Invoke your function
Hello
operation result:
$./test.sh
Hello World
$
To call a function, you only need to give the function name without parentheses.
Let's look at a function with a return statement:
#!/bin/bash
funWithReturn(){
echo "The function is to get the sum of two numbers..."
echo -n "Input first number: "
read aNum
echo -n "Input another number: "
read anotherNum
echo "The two numbers are $aNum and $anotherNum !"
return $(($aNum+$anotherNum))
}
funWithReturn
# Capture value returnd by last command
ret = $?
echo "The sum of two numbers is $ret !"
operation result:
The function is to get the sum of two numbers...
Input first number: 25
Input another number: 50
The two numbers are 25 and 50 !
The sum of two numbers is 75 !
The function return value is obtained by $? after calling the function.
Let's look at another example of function nesting:
#!/bin/bash
# Calling one function from another
number_one () {
echo "Url_1 is http://see.xidian.edu.cn/cpp/shell/"
number_two
}
number_two () {
echo "Url_2 is http://see.xidian.edu.cn/cpp/u/xitong/"
}
number_one
operation result:
Url_1 is http://see.xidian.edu.cn/cpp/shell/
Url_2 is http://see.xidian.edu.cn/cpp/u/xitong/
Deleting a function can also use the unset command like deleting a variable, but with the .f option, as follows:
$unset .f function_name
If you want to call the function directly from the terminal, you can define the function in the .profile file in the home directory, so that every time you log in, you can immediately call the function by typing the function name after the command prompt.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325721920&siteId=291194637