Latest web development tutorials

Shell function

linux shell can be user-defined functions, then you can easily call in shell scripts.

Definition Format the shell function as follows:

[ function ] funname [()]

{

    action;

    [return int;]

}

Explanation:

  • 1, can take function fun () is defined to be a direct fun () is defined, without any parameters.
  • 2, return parameters can be displayed plus: return return, if not, the result will be the last command run, as the return value. followed by the return value of n (0-255

The following example defines a function and call:

#!/bin/bash
# author:本教程
# url:www.w3big.com

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

Output:

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

The following definition of a function with a return statement:

#!/bin/bash
# author:本教程
# url:www.w3big.com

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 this function through $? Is 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 the function using only its function name.


Function Arguments

In the Shell, you can pass parameters to which the function is called. In the function body inside, to get the value of a parameter in the form of $ n, for example, $ 1 for the first argument, $ 2 for the second parameter ...

Example function with parameters:

#!/bin/bash
# author:本教程
# url:www.w3big.com

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 can not get a tenth parameters, get the tenth parameters need $ {10}. When n> = 10, you need to use $ {n} to obtain the parameters.

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

Parameter handling Explanation
$ # The number of parameters passed to the script
$ * In a single string displays all parameters passed to the script
$$ The current process ID number of the script runs
$! Finally, a process running in the background ID number
$ @ * $ With the same, but when you use quotation marks, and returns each parameter in quotes.
$ - Shell displays the current option to use the same function set command.
$? Displays exit status of the last command. 0 indicates no errors, and any other value indicates an error.