Shell chapter seven function

A function definition

Linux shell can be user-defined functions, then you can easily call in a shell script.

Definition Format the shell functions as follows:

[ function ] funname [()]
{
    action;
    [return int;]
}

Description:

  • 1, can function with fun () is defined, may be directly fun () is defined, without any parameters.
  • 2, the parameters return can be displayed add: return returned, if not, will last a result of that command, as a return value. followed by the return value of n (0-255).


Second, examples

The following example defines a function and call:

#!/bin/bash

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

Output:

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


Defined below having a function return statement:

#!/bin/bash

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


Output similar to the following:

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

Function return value after calling the function through $? Be obtained.

Note: All functions must be defined before use. This means that the function must be placed in the beginning of the script until the shell interpreter when it was first discovered, it can be used. Call functions using only its function name.


Third, the function parameters

In the Shell, the parameters that can be passed when calling the function. Inside the function thereof, to obtain the value of the parameter $ n the form of, e.g., $ 1 represents the first parameter, the second parameter represents $ 2 ...

Sample function 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 10th parameter can not be obtained, obtain parameters required tenth {10} $. When n> = 10, requires the use of $ {n} to obtain the parameters.


In addition, there are several parameters to handle special characters:

Parameter Handling Explanation
$# Number of arguments passed to the script
$* Show all parameters passed to the script to a single string
$$ The script runs the current process ID number
$! Finally, a process running in the background ID number
$@ $ * The same, but the use of quotation marks, and returns each parameter in quotation marks.
$- Shell used to display the current option, and set the same command function.
$? Displays exit status of the last command. 0 means no error, any other value indicates an error.


reference:

Shell function


Guess you like

Origin www.cnblogs.com/linuxAndMcu/p/11121014.html