08Shell函数

Shell函数

1.完成特定功能的代码片段(块)

2.在 shell 中定义函数可以使用代码模块化,便于复用代码

3.函数必须先定义才可以使用

传参 $1,$2

变量 local

返回值 return $?  

定义函数

方法1

函数名() {
	函数要实现的功能代码
}  

方法2

function 函数名 {
	函数要实现的功能代码
}  

调用函数

函数名
函数名 参数 1 参数 2  

注意

`函数名` -->这种调用函数的方式是在子shell中执行
函数名   -->这种调用函数的方式是在当前shell中执行

示例

[root@hadoop04 shell_function]# vim factorial.sh
#!/bin/env bash

factorial(){
factorial=1
# 此处的$1是调用函数时传入的第一个位置参数
#for((i=1;i<=$1;i++))
for i in `seq $1`
do
        #factorial=$[ $factorial * $i ]
        #let factorial=$factorial*$i
        let factorial*=$i
done
echo "$1的阶乘:${factorial}"
}

# 此处的$1是脚本执行时传入的第一个位置参数
factorial $1
#factorial $2
#factorial $3


[root@hadoop04 shell_function]# bash factorial.sh
5的阶乘:120

返回值

函数的返回值是函数最后一条命令执行的状态码

自定义返回值,数值范围:0-255

return 自定义返回值

输出任何任何值

echo XXX
# 使用 echo XXX 作为函数的输出,然后使用变量去接收函数的输出,XXX可以是字符串、整数、浮点数...只要能echo的都可以

示例

例1

[root@hadoop04 shell_function]# vim func_return_out.sh
#!/bin/bash

func_return(){
read -p "enter num:" num
# 函数输出
echo $[2*$num]
#let num=2*$num
#return $num
}

# 接收函数的输出
result=`func_return`

echo "func_return return value: ${result}"

[root@hadoop04 shell_function]# bash func_return_out.sh 
enter num:2000
func_return return value: 4000

例2

[root@hadoop04 shell_function]# vim func_output_array.sh
#!/bin/bash

num=(1 2 3 4 5)

func_array() {
        local newarray=($*)
        # 也可以使用 local newarray=(`echo $*`)
        local i
        # $#指的是传入参数的个数
        for((i=0;i<$#;i++))
        do
                newarray[$i]=$[ ${newarray[$i]} * 5 ]
        done
        echo ${newarray[*]}
}

func_array ${num[*]}
# 可以通过变量接收函数的输出
#result=`func_array ${num[*]}`
#echo ${result[*]}

[root@hadoop04 shell_function]# bash func_output_array.sh
5 10 15 20 25

函数传参

1.函数接收位置参数 $1 [$2 ... $n]

2.函数接收数组变量 $* 或者 $@

3.函数将接收到的所有参数赋值给数组 newarray=($*) 或者 newarray=(echo $*)

位置参数

函数名  $1 [$2 ... $n] 

示例

[root@hadoop04 shell_function]# vim parameter.sh 
#!/bin/bash
if [ $# -ne 3 ];then
	echo "usage: `basename $0` par1 par2 par3"
	exit
fi

func_test() {
    # 此处的$1 $2 $3是调用函数时传入的位置参数
	echo "$(($1 * $2 * $3))"
}

# 此处的$1 $2 $3是脚本执行时传入的位置参数
result=`func_test $1 $2 $3`

echo "result is ${result}"

[root@hadoop04 shell_function]# bash parameter.sh 1 2 3
result is 6

数组变量

函数名  ${数组名[@]}
函数名  ${数组名[*]}

示例

[root@hadoop04 shell_function]# vim func_array.sh
#!/bin/bash

num=(1 2 3 4 5)

func_array() {
		# local 变量名=变量值
		# local factorial=1 只在当前函数生效
        factorial=1
        for i
        #相当于 for i in "$@"
        #或者 相当于 for i in $*
        do
                factorial=$[factorial * $i]
        done
        echo "${factorial}"
}

# 给函数func_array传入数组的所有元数值
func_array ${num[@]}

[root@hadoop04 shell_function]# bash func_array.sh 
120

本地变量

local 变量名=变量值
# 只在当前函数生效,不会影响脚本中的其他函数以及变量

示例

[root@hadoop04 shell_function]# vim func_output_array1.sh
#!/bin/bash

num=(1 2 3)

func_array() {
        local outarray=()
        local j
        for i
        #相当于 for i in "$@"
        #或者 相当于 for i in $*
        do
                outarray[j++]=$[$i*5]
        done
        echo ${outarray[*]}
}

# `函数名` 这种调用函数的方式是在子shell中执行
# 函数名 这种调用函数的方式是在当前shell中执行
result=`func_array ${num[*]}`
echo ${result[*]}

[root@hadoop04 shell_function]# bash func_output_array1.sh
5 10 15
posted on 2020-01-06 14:28  D小调的微笑  阅读(228)  评论(0编辑  收藏  举报