Getting Started with Shell Scripts_11

Shell functions

The linux shell can user-defined functions, which can then be called freely in shell scripts.

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

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

illustrate:

  • 1. It can be defined with function fun() or directly with fun() without any parameters.
  • 2. When the parameter is returned, it can be displayed and added: return is returned. If it is not added, the result of the last command will be used as the return value. return followed by the value n (0-255

The following example defines a function and calls it:

#!/bin/bash
# author:ethan

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

输出结果:
-----函数开始执行-----
这是我的第一个 shell 函数!
-----函数执行完毕-----

The following defines a function with a return statement:

#!/bin/bash
# author:ethan

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

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

The function return value is obtained by $? after calling the function.

Note: All functions must be defined before use. This means that the function must be placed at the beginning of the script and not available until the shell interpreter first discovers it. A function is called using only its function name.

function parameter

In the shell, you can pass arguments to a function when you call it. Inside the function body, the value of the parameter is obtained in the form of $n, for example, $1 represents the first parameter, $2 represents the second parameter...

Example of a function with parameters:

#!/bin/bash
# author:ethan

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

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

Note that $10 cannot get the tenth parameter, ${10} is required to get the tenth parameter . When n>=10, you need to use ${n} to get the parameters .

In addition, there are several special characters used to process parameters:

parameter handling illustrate
$# The number of arguments passed to the script
$* Displays all parameters passed to the script as a single string
$$ The current process ID number of the script running
$! ID number of the last process running in the background
$@ Same as $*, but used with quotes, returning each argument in quotes.
$- Displays the current options used by the shell, which has the same function as the set command.
$? Displays the exit status of the last command. 0 means no error, any other value means there is an error.

 

Guess you like

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