函数:
通过使用函数,可以对程序进行更加好的组织。将一些相对独立的代码变成函数,
可以提高程序的可读性和重用性。避免重复编写大量相同的代码。
1.语法
方法1:
函数名() {
函数体
return n
}
方法2:(更加规范)
function 函数名() {
函数体
return n
}
[root@localhost mnt]# vim 01_function.sh
#####################
#!/bin/bash
function fun1(){
echo "hello shell!"
}
fun1
[root@localhost mnt]# sh 01_function.sh
hello shell!
2.调用函数
[root@localhost mnt]# vim 02_function.sh
#####################
#!/bin/bash
function fun1() {
echo "I like you"
}
count=1
while [ $count -le 5 ]
do
fun1
((count++))
done
echo "Eed of loop"
fun1
echo "Eed of scritp"
# 若是加-x表示调试
[root@localhost mnt]# sh 02_function.sh
3.引用函数
每次引用函数时,bash会重新回到函数的定义
函数不一定要要在最开始定义,但是如果函数在定义之前便使用会报错
[root@localhost mnt]# vim 03_function.sh
#####################
#!/bin/bash
count=1
echo "Before function definition"
function fun1(){
echo "hello fun1"
}
while [ $count -le 5 ]
do
fun1
count=$[ $count + 1 ]
done
echo "End of loop"
fun2
function fun2() {
echo "hello fun2"
}
[root@localhost mnt]# sh 03_function.sh
函数名必须是唯一的,如果重复定义了有相同名字的函数,则新定义的函数就会覆盖旧的函数
[root@localhost mnt]# vim 04_function.sh
#####################
#!/bin/bash
function fun1(){
echo "first fun1"
}
function fun1(){
echo "second fun1"
}
function fun1(){
echo "third fun1"
}
fun1
[root@localhost mnt]# sh 04_function.sh
third fun1
4.返回值
(1)默认退出状态码
默认退出状态码:默认情况下,函数退出状态码是函数中最后一条命令返回的退出状态码
[root@localhost mnt]# ls -l westosfile
ls: cannot access westosfile: No such file or directory
[root@localhost mnt]# vim func_exit_01.sh
#####################
#!/bin/bash
function fun1(){
echo "Trying to display a non-existent file"
ls -l westosfile &> /dev/null
}
echo "test the function"
fun1
echo "The exit status is: $?"
# $? 在循环中表示获取循环体中最后一条语句执行状态的返回值
0表示成功,非0表示失败
[root@localhost mnt]# sh func_exit_01.sh
##调换语句
[root@localhost mnt]# vim func_exit_01.sh
#####################
#!/bin/bash
function fun1(){
ls -l westosfile &> /dev/null
echo "Trying to display a non-existent file" ##输出语句一定是正确的
}
echo "test the function"
fun1
echo "The exit status is: $?"
[root@localhost mnt]# sh func_exit_01.sh
(2)return命令
shell使用return命令来退出函数并返回特定的退出状态码
自定义返回值范围: 0-255之间
[root@localhost mnt]# vim func_exit_return_01.sh
#####################
#!/bin/bash
function func(){
read -p "Enter a value: " value
echo "doubling the value..."
return $[ $value * 2 ] ##用return自定义返回值
}
func
echo "The new value is $?"
[root@localhost mnt]# sh func_exit_return_01.sh
(3)函数输出
使用函数输出将函数的输出保存在shell变量中,可以获得任何类型的函数输出,并将其保存到变量中
[root@localhost mnt]# vim function_variate.sh
#####################
#!/bin/bash
function db1() {
read -p "Enter a value:" value
echo $[ $value * 2 ]
}
result=`db1`
echo "The new value is $result"
[root@localhost mnt]# sh function_variate.sh
(4)函数中的变量
函数中的变量:可以向函数传递参数
练习1:
定义算法,当脚本后什么都不输入时,输出-1,当脚本后输入一个数字时,把这个数字扩大2倍,当输入两个数字时,把这两个数字相加
[root@localhost mnt]# vim function_value_01.sh
#####################
#!/bin/bash
a=$1 #接收脚本后的第一串字符
b=$2 #接收脚本后的第二串字符
c=$# ##记录脚本后输入字符的个数
function add() {
if [ "$c" == 0 ];then
echo "-1"
elif [ "$c" == 1 ];then
echo $[ a + a ]
elif [ "$c" == 2 ];then
echo $[ a + b ]
else
echo "error"
fi
}
add
[root@localhost mnt]# sh function_value_01.sh
练习2:
定义算法,当用户的输入不符合输入要求时,告诉用户具体用法,当输入正确时,计算乘积
[root@localhost mnt]# vim function_value_02.sh
#####################
#!/bin/bash
function fun1() {
echo $[ $1 * $2 ]
}
if [ $# -eq 2 ];then
value=`fun1 $1 $2`
echo "The result is $value"
else
echo "Usage:fun1 a b"
fi
# $# 表示脚本后输入字符的个数
[root@localhost mnt]# sh function_value_02.sh
(5)函数不能从命令行获取脚本的参数值
[root@localhost mnt]# vim function_value_03.sh
#####################
#!/bin/bash
read -p "Enter a value:" value
function fun1(){
db1=$[ $value * 2 ]
}
fun1
echo "The new value is:" $value
5.变量的作用域
(1)全局变量
全局变量:定义在函数体之外,任何地方都生效的变量,默认情况下,脚本主体内定义全局变量,函数内可以用,函数外也可以
[root@localhost mnt]# vim function_all_01.sh
#####################
#!/bin/bash
function fun1(){
temp=$[ $value + 5 ]
result=$[ $temp *2 ]
}
# 定义全局变量
temp=4
value=6
fun1
echo "The result is $result"
if [ $temp -gt $value ];then
echo "temp is lager"
else
echo "temp is smaller"
fi
[root@localhost mnt]# sh function_all_01.sh
(2)局部变量
局部变量:定义在函数体内部,仅在函数内部可以使用
[root@localhost mnt]# vim function_local_01.sh
#####################
#!/bin/bash
function fun1(){
local temp=$[ $value + 5 ] # 声明temp为局部变量
result=$[ $temp *2 ]
}
temp=4
value=6
fun1
echo "The result is $result"
if [ $temp -gt $value ];then
echo "temp is lager"
else
echo "temp is smaller"
fi
[root@localhost mnt]# sh function_local_01.sh
6.函数的递归
计算阶乘:
注意: 0!=1 1! =1
[root@localhost mnt]# vim function_factorial.sh
#####################
#!/bin/bash
function jc(){
if [ $1 -eq 1 ];then
echo 1
elif [ $1 -eq 0 ];then
echo 1
else
local temp=$[ $1 - 1 ]
local result=`jc $temp`
echo $[ result * $1 ]
fi
}
read -p "Enter a value: " value
result=`jc $value`
echo "The jc of $value is: $result"
[root@localhost mnt]# sh function_factorial.sh