Unix 简明教程
Unix / Linux - Shell Functions
在本章中,我们将详细讨论 Shell 函数。使用函数可以将脚本的整体功能分解为更小的逻辑子部分,然后根据需要调用它们来执行各自的任务。
In this chapter, we will discuss in detail about the shell functions. Functions enable you to break down the overall functionality of a script into smaller, logical subsections, which can then be called upon to perform their individual tasks when needed.
使用函数执行重复任务是创建 code reuse 的绝佳方式。这是现代面向对象编程原则的重要组成部分。
Using functions to perform repetitive tasks is an excellent way to create code reuse. This is an important part of modern object-oriented programming principles.
Shell 函数类似于其他编程语言中的子例程、过程和函数。
Shell functions are similar to subroutines, procedures, and functions in other programming languages.
Creating Functions
要声明函数,只需使用以下语法:
To declare a function, simply use the following syntax −
function_name () {
list of commands
}
函数的名称是 function_name ,您将在脚本中的其他地方使用它来调用此函数。函数名称后面必须紧跟括号,然后是一对大括号中包含的命令列表。
The name of your function is function_name, and that’s what you will use to call it from elsewhere in your scripts. The function name must be followed by parentheses, followed by a list of commands enclosed within braces.
Pass Parameters to a Function
您可以在调用函数时定义一个接受参数的函数。这些参数将由 $1 、 $2 等表示。
You can define a function that will accept parameters while calling the function. These parameters would be represented by $1, $2 and so on.
以下是一个示例,其中我们传递两个参数 Zara 和 Ali,然后在函数中捕获并打印这些参数。
Following is an example where we pass two parameters Zara and Ali and then we capture and print these parameters in the function.
#!/bin/sh
# Define your function here
Hello () {
echo "Hello World $1 $2"
}
# Invoke your function
Hello Zara Ali
执行后,您会收到以下结果 −
Upon execution, you will receive the following result −
$./test.sh
Hello World Zara Ali
Returning Values from Functions
如果您从函数内部执行 exit 命令,其效果不仅会终止函数的执行,还会终止调用该函数的 shell 程序。
If you execute an exit command from inside a function, its effect is not only to terminate execution of the function but also of the shell program that called the function.
如果您只想终止函数的执行,则有方法退出已定义的函数。
If you instead want to just terminate execution of the function, then there is way to come out of a defined function.
根据具体情况,您可以使用 return 命令从函数返回任何值,其语法如下:
Based on the situation you can return any value from your function using the return command whose syntax is as follows −
return code
此处 code 可以是您选择的内容,但显然您应该选择在整个脚本上下文中具有意义或有用的内容。
Here code can be anything you choose here, but obviously you should choose something that is meaningful or useful in the context of your script as a whole.
Example
以下函数返回一个值 10:
Following function returns a value 10 −
#!/bin/sh
# Define your function here
Hello () {
echo "Hello World $1 $2"
return 10
}
# Invoke your function
Hello Zara Ali
# Capture value returnd by last command
ret=$?
echo "Return value is $ret"
执行后,您会收到以下结果 −
Upon execution, you will receive the following result −
$./test.sh
Hello World Zara Ali
Return value is 10
Nested Functions
函数一个更有趣的功能是它们可以调用它们自己以及其他函数。调用自身的函数称为 recursive function 。
One of the more interesting features of functions is that they can call themselves and also other functions. A function that calls itself is known as a recursive function.
以下示例演示两个函数的嵌套 –
Following example demonstrates nesting of two functions −
#!/bin/sh
# Calling one function from another
number_one () {
echo "This is the first function speaking..."
number_two
}
number_two () {
echo "This is now the second function speaking..."
}
# Calling function one.
number_one
执行后,您会收到以下结果 −
Upon execution, you will receive the following result −
This is the first function speaking...
This is now the second function speaking...
Function Call from Prompt
可以在 .profile 内放置常用函数的定义。每当你登录时,这些定义都会可用,你可以在命令提示符中使用这些定义。
You can put definitions for commonly used functions inside your .profile. These definitions will be available whenever you log in and you can use them at the command prompt.
或者,你可以将定义分组放在一个文件中,称为 test.sh ,然后通过键入以下内容在当前 shell 中执行文件 –
Alternatively, you can group the definitions in a file, say test.sh, and then execute the file in the current shell by typing −
$. test.sh
这会产生一个效果,就是由 test.sh 定义的函数被读入并定义到当前 shell,如下所示 –
This has the effect of causing functions defined inside test.sh to be read and defined to the current shell as follows −
$ number_one
This is the first function speaking...
This is now the second function speaking...
$
若要从 shell 中删除函数定义,请使用 unset 命令和 .f 选项。此命令还用于删除对 shell 的变量定义。
To remove the definition of a function from the shell, use the unset command with the .f option. This command is also used to remove the definition of a variable to the shell.
$ unset -f function_name