创建函数
在 bash shell 脚本中创建函数的语法有两种
第一种使用关键字 function,随后跟上分配给该代码块的函数名:
- function name {
- commands
- }
name 定义了该函数的唯一名称。脚本中的函数名不能重复。
commands 是组成函数的一个或多个 bash shell 命令。调用该函数时,bash shell 会依次执行函数内的命令,就像在普通脚本中一样。
第二种在 bash shell 脚本中创建函数的语法更接近其他编程语言中定义函数的方式:
- name() {
- commands
- }
函数名后的空括号表明正在定义的是一个函数。这种语法的命名规则和第一种语法一样。
使用函数
要在脚本中使用函数,只需像其他 shell 命令一样写出函数名即可:
- $ cat test1
- #!/bin/bash
- # using a function in a script
- function func1 {
- echo "This is an example of a function"
- }
- count=1
- while [ $count -le 5 ]
- do
- func1
- count=$[ $count + 1 ]
- done
- echo "This is the end of the loop"
- func1
- echo "Now this is the end of the script"
- $
- $ ./test1
- This is an example of a function
- This is an example of a function
- This is an example of a function
- This is an example of a function
- This is an example of a function
- This is the end of the loop
- This is an example of a function
- Now this is the end of the script
- $
函数定义不一定非要放在 shell 脚本的最开始部分,但是要注意这种情况。如果试图在函数被定义之前调用它,则会收到一条错误消息:
- $ cat test2
- #!/bin/bash
- # using a function located in the middle of a script
- count=1
- echo "This line comes before the function definition"
- function func1 {
- echo "This is an example of a function"
- }
- while [ $count -le 5 ]
- do
- func1
- count=$[ $count + 1 ]
- done
- echo "This is the end of the loop"
- func2
- echo "Now this is the end of the script"
- function func2 {
- echo "This is an example of a function"
- }
- $
- $ ./test2
- This line comes before the function definition
- This is an example of a function
- This is an example of a function
- This is an example of a function
- This is an example of a function
- This is an example of a function
- This is the end of the loop
- ./test2: func2: command not found
- Now this is the end of the script
函数名必须是唯一的,否则就会出问题。如果定义了同名函数,
那么新定义就会覆盖函数原先的定义,而这一切不会有任何错误消息:
- $ cat test3
- #!/bin/bash
- # testing using a duplicate function name
- function func1 {
- echo "This is the first definition of the function name"
- }
- func1
- function func1 {
- echo "This is a repeat of the same function name"
- }
- func1
- echo "This is the end of the script"
- $
- $ ./test3
- This is the first definition of the function name
- This is a repeat of the same function name
- This is the end of the script
- $