Shell Scripting Tutorial [9] - Shell Function

Shell Scripting Tutorial [9] - Shell Function


Directory : https://blog.csdn.net/shn111/article/details/131590488

Reference tutorial : https://www.runoob.com/linux/linux-shell.html

Online editor : https://www.runoob.com/try/runcode.php?filename=helloworld&type=bash


The linux shell can user-defined functions, and then they can be called casually in shell scripts.

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

[ function ] funname [()]

{
    
    

    action;

    [return int;]

}
  • It can be defined with function fun(), or directly defined with fun() without any parameters.
  • Parameter return can be explicitly added: return return, if not added, the result of the last command will be used as the return value. return followed by the value n(0-255

Example:

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

function with return statement

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

The function return value is obtained by 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 until it is first seen by the shell interpreter before it can be used. A function is called using only its function name.


function parameters

In the shell, parameters can be passed to functions when they are called. Inside the function body, the value of the parameter is obtained in the form of $n, for example, $1 represents the first parameter, and $2 represents the second parameter.

Example:

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 needed 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:
(The following content is explained in detail in the previous Shell parameter passing)

Parameter handling illustrate
$# The number of arguments passed to the script or function
$* Displays all arguments passed to the script as a single string
$$ The current process ID number the script is running under
$! The ID number of the last process running in the background
$@ Same as $*, but use quotes and return each argument in quotes
$- Display the current options used by the Shell, which has the same function as the set command
$? Display the exit status of the last command. 0 indicates no errors, any other value indicates errors

Guess you like

Origin blog.csdn.net/shn111/article/details/131591074