函数是被赋予名称的脚本代码块,可以在代码的任意位置重用。当需要在脚本中使用代码块时,只需要在代码块引用被赋予的函数名称(调用函数)!
创建函数格式1:
funcion name { commands }
name属性定义该函数的唯一名称。脚本中自定义的每个函数都必须赋予唯一名称!
command是组成函数的一条或者多条bash shell命令
创建函数格式2:
name() { commands }
使用函数:
#!/bin/bash
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 loop"
func1
echo "Now this is the end of the script"
[root@localhost ~]# ./f1.sh
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 loop
This is an example of a function
Now this is the end of the script
转载于:https://blog.51cto.com/liangey/1575355