函数

函数

是命令的集合 能完成特定功能的代码块
可以复用
函数和变量类似 只有先定义才可以调用 如果只定义不调用 则不会执行

1.函数的定义及调用

三种方法:

[root@shell /server/scripts]# sh fun.sh 
#!/bin/sh
test1(){
	echo "第一种函数定义方式"
}

function test2(){
	echo "第二种函数定义方式"
}

function test3 {
	echo "第三种函数定义方式"
}

直接调用就可以
test1
test2
test3

[root@shell /server/scripts]# sh fun.sh 
第一种函数定义方式
第二种函数定义方式
第三种函数定义方式

想直接执行test1怎么办?

[root@shell /server/scripts]# source fun.sh  加载到父shell中执行 相当于定义了
第一种函数定义方式
第二种函数定义方式
第三种函数定义方式
[root@shell /server/scripts]# test1
第一种函数定义方式
[root@shell ~]# vim fun.sh
#!/bin/bash
fun (){
    echo '123'
}
fun                                              
[root@shell ~]# sh fun.sh
123


[root@shell ~]# vim fun.sh
 #!/bin/bash
fun (){
       echo '123'
}
rc=$(fun)                                        
echo $rc
[root@shell ~]# sh fun.sh
123

2.函数的传参

不能直接传参

1.在函数名的后面跟参数

2.全局配置 在函数的最上面 设置变量

3.local 只在函数体内部生效

[root@shell /server/scripts]# cat fun.sh
#!/bin/bash
fun(){
	if [ -f $1 ];then
		echo "$1 is exists"
	else
		echo "$1 is no exists"
	fi
}
fun $1        在后面跟上参数

[root@shell /server/scripts]# sh fun.sh /etc/hosts
/etc/hosts is exists

#########################################################

[root@shell /server/scripts]# cat fun.sh
#!/bin/bash
fun(){
	if [ -f $1 ];then
		echo "$1 is exists"
	else
		echo "$1 is no exists"
	fi
}
fun $2 $3        如果这里变成$2 $3

[root@shell /server/scripts]# sh fun.sh /etc/hosts /etc/passwd
/etc/passwd is exists
[root@shell /server/scripts]# sh fun.sh
#!/bin/bash
##############################################################
# File Name: fun.sh
# Time: 2019-11-05-19:49:34
# Author: msy
##############################################################
fun(){
num=20
for i in `seq 10`
do
    sum=$[num+i]
done
echo $sum
}
fun    

[root@shell /server/scripts]# sh fun.sh
30
[root@web scripts]# cat fun1.sh
#!/bin/sh
fun(){
num=20
for i in `seq $1`
do
	sum=$[num+i]
done
	echo $sum
}
fun $3 $1 $2

[root@shell /server/scripts]# sh fun.sh 10 20 30
50


fun(){
num=20
for i in `seq $2`                                                     
do
       sum=$[num+i]
done
echo $sum
}
fun $3 $1 $2

[root@shell ~]# sh fun.sh 10 20 30 
30


[root@shell /server/scripts]# cat fun.sh
# File Name: fun.sh
# Time: 2019-11-05-19:49:34
# Author: msy
##############################################################
fun(){
num=20
for i in `seq $1`
do
    sum=$[num+i]
done
echo $sum
}
fun $3
fun $1
fun $2                             

[root@shell /server/scripts]# sh fun.sh 10 20 30
50
30
40

local

[root@shell /server/scripts]# cat fun.sh
#!/bin/bash
##############################################################
# File Name: fun.sh
# Time: 2019-11-05-19:49:34
# Author: msy
##############################################################
fun(){
local num=20             用了local!!!注意 只针对函数体内生效                     
for i in `seq $1`
do
    sum=$[num+i]
done
echo $sum
}
fun $1
echo $num

[root@shell /server/scripts]# sh fun.sh 10
30


[root@shell /server/scripts]# cat fun.sh
#!/bin/bash
##############################################################
# File Name: fun.sh
# Time: 2019-11-05-19:49:34
# Author: msy
##############################################################
fun(){
num=20             不用local!!!                     
for i in `seq $1`
do
    sum=$[num+i]
done
echo $sum
}
fun $1
echo $num

[root@shell /server/scripts]# sh fun.sh 10
30
20

3.函数的返回值

#!/bin/sh
[ -f /etc/hosts ] && exit 50 || exit 100
	
#!/bin/sh
fun(){
	if [ -f $1 ];then
		return 50
	else
		return 100
	fi
}
fun $1
re=$?
[ $re -eq 50 ] && echo "$1 该文件存在"
[ $re -eq 100 ] && echo "$1 该文件不存在"

[root@web scripts]# cat fun3.sh 
#!/bin/sh
fun(){
	if [ -f $1 ];then
	    return 50
	else
		return 100
	fi	
}
fun $1
if [ $? -eq 50 ];then
		echo "ok"
else
		echo "error"
fi
`函数内如果执行的上一条是函数名  则不会返回函数本身执行的返回值

[root@web scripts]# cat fun4.sh
#!/bin/sh
fun(){
	  echo 50
	  return 1
}
re=`fun`                  在函数里只要是和fun相关的 就不会读 返回值就不是0  主要是赋值有问题
                          但是在下面加上name=oldboy 就会读去fun 返回值为0
echo "函数的返回值是?: $?"
echo "函数执行的结果是?: $re"

[root@web scripts]# sh fun4.sh
函数的返回值是?: 1
函数执行的结果是?: 50

[root@web scripts]# cat fun4.sh
#!/bin/sh
fun(){
	  echo 50
	  return 1
}
re=`fun`
name=oldboy
echo "函数的返回值是?: $?"
echo "函数执行的结果是?: $re"

[root@web scripts]# sh fun4.sh
函数的返回值是?: 0
函数执行的结果是?: 50
posted @ 2019-11-18 20:46  干瘪的柠檬  阅读(217)  评论(0)    收藏  举报