Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The shell function


May 22, 2021 Linux


Table of contents


The shell function

Linux shells can be user-defined functions, which can then be called at will in shell scripts.

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

[ function ] funname [()]

{

    action;

    [return int;]

}

Description:

  • 1, can be with the fun () definition, can also be directly fun() definition, without any parameters.
  • 2, parameter return, you can show plus: return return, if not, will be the last command to run the result, as a return value. R eturn followed by the value n (0-255).

The following example defines a function and calls it:

#!/bin/bash
demoFun(){
    echo "这是我的第一个 shell 函数!"
}
echo "-----函数开始执行-----"
demoFun
echo "-----函数执行完毕-----"

Output:

-----函数开始执行-----
这是我的第一个 shell 函数!
-----函数执行完毕-----

Here's a function with a return statement:

#!/bin/bash
funWithReturn(){
    echo "这个函数会对输入的两个数字进行相加运算..."
    echo "输入第一个数字: "
    read aNum
    echo "输入第二个数字: "
    read anotherNum
    echo "两个数字分别为 $aNum 和 $anotherNum !"
    return $(($aNum+$anotherNum))
}
funWithReturn
echo "输入的两个数字之和为 $? !"

The output is similar to the following:

这个函数会对输入的两个数字进行相加运算...
输入第一个数字: 
1
输入第二个数字: 
2
两个数字分别为 1 和 2 !
输入的两个数字之和为 3 !

The function returns a value that passes $after the function is called? t o get.

Note: All functions must be defined before they can be used. T his means that the function must be placed at the beginning of the script until the shell interpreter first discovers it. C all a function using only its function name.


Function arguments

In Shell, you can pass arguments to a function when it is called. I nside the function body, the value of the argument is obtained in the form of $n, for example, $1 represents the first argument and $2 represents the second argument...

Examples of functions with parameters:

#!/bin/bash
funWithParam(){
    echo "第一个参数为 $1 !"
    echo "第二个参数为 $2 !"
    echo "第十个参数为 $10 !"
    echo "第十个参数为 ${10} !"
    echo "第十一个参数为 ${11} !"
    echo "参数总数有 $# 个!"
    echo "作为一个字符串输出所有参数 $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

Output:

第一个参数为 1 !
第二个参数为 2 !
第十个参数为 10 !
第十个参数为 34 !
第十一个参数为 73 !
参数总数有 11 个!
作为一个字符串输出所有参数 1 2 3 4 5 6 7 8 9 34 73 !

Note that $10 does not get the 10th argument, and $10 is required to get the {10}. W hen n is 10, you need to use ${n to get the parameters.

In addition, there are several special characters for handling parameters:

Argument processing Description
$# The number of parameters passed to the script
$* Displays all parameters passed to the script in a single string
$$ The current process ID number that the script is running
$! The ID number of the last process running in the background
$@ Same as $, but with quotation marks, and returns each argument in quotation marks.
$- Displays the current options used by Shell, with the same functionality as the set command.
$? Displays the exit status of the last command. 0 indicates no error, and any other value indicates an error.