12.30 shell--函数,变量的作用域,函数的递归调用

函数

1.语法

方法1:

函数名() {
函数体
return n
}

方法2:

function 函数名() {
函数体
return n
}

2.调用函数

实验一:编写一个函数,并调用

[root@localhost mnt]# cat function1.sh
#! /bin/bash
function fun1(){
echo "i like redhat"
}
fun1
[root@localhost mnt]# bash function1.sh
i like redhat

3.引用函数

#每次引用函数时,bash会重新回到函数的定义

4.函数不一定要在最开始定义,但是如果函数在定义前就使用,会报错

实验:编写两个函数,一个在调用前定义,一个在调用后定义

[root@localhost mnt]# cat function2.sh
#! /bin/bash
function fun1(){
echo "before use"
}
fun1
fun2
function fun2(){
echo "after use"
}
[root@localhost mnt]# sh function2.sh
before use
function2.sh: line 6: fun2: command not found

5.函数名必须是唯一的,如果重新定义了函数,新的函数会覆盖旧的

实验:定义两个函数查看结果

[root@localhost mnt]# cat function3.sh
#! /bin/bash
function fun1(){
echo "first define fun1"
}
function fun1(){
echo "second define fun1"
}
fun1

[root@localhost mnt]# sh function3.sh
second define fun1

6.返回值

1)默认退出状态码

默认情况下,函数的退出状态码是函数中最后一条命令返回的退出状态码

成功为0,不成功为非0

实验:测试函数返回为不成功的命令

[root@localhost mnt]# cat function4.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 : $?"
[root@localhost mnt]# sh function4.sh
test the function:
trying to display a non-existent file
The exit status is : 2

实验2:测试函数返回为成功的命令

[root@localhost mnt]# cat function5.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 function5.sh
test the function:
trying to display a non-existent file
The exit status is : 0

2)使用return命令

shell使用return命令来退出函数并返回特定的退出状态码

实验

[root@localhost mnt]# cat function6.sh
#!/bin/bash

function db1() {
read -p "Enter a value:" value
echo "doubling the value..."
return $[ $value * 2 ]
}

db1

echo "The new value is $?"                           ###$? 代表上一条命令的返回值
[root@localhost mnt]# sh function6.sh
Enter a value:5
doubling the value...
The new value is 10

3)使用函数输出

将函数的输出保存在shell变量中
可以获得任何类型的函数输出
并将其保存到变量中

实验

[root@localhost mnt]# cat function7.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 function7.sh
Enter a value:5
The new value is 10

4)函数中使用变量

可以向函数中传递参数
函数名会再$0变量中定义,函数命令行上的任何参数都会通过$1,$2定义
$#来判断传给函数的参数数目

实验:输入两个参数,求和

[root@localhost mnt]# cat function9.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 function9.sh 2 3
The result is 6

5)函数不能直接从命令行获取脚本的参数值

变量的作用域
1.全局变量

任何地方都生效的变量默认情况下,脚本主体内定义全局变量,函数内可以用,函数外也可以用

实验:value为全局变量。在函数中的value改变,都改变

[root@localhost mnt]# cat overall.sh
#!/bin/bash

function db1() {
value=$[ $value * 2 ]
}

read -p "Enter a value:" value
db1
echo "The new value is:" $value
[root@localhost mnt]# sh overall.sh
Enter a value:5
The new value is: 10

2.局部变量

定义方法: local value

实验:

[root@localhost mnt]# cat overall2.sh
#!/bin/bash

function fun1() {
local temp=$[ $value + 5 ]
echo $temp
}

temp=4
value=6

funtemp=`fun1`
echo "over temp : $temp"
echo "local remp: $funtemp"

[root@localhost mnt]# sh overall2.sh
over temp : 4
local remp: 11

函数递归

计算阶乘

实验:单个参数,输入值valuse作为函数的参数,进行阶乘运算

[root@localhost mnt]# cat function8.sh
#!/bin/bash

function jc() {
if [ $1 -eq 1 ];then                                 ##如果为1的话阶乘为1
echo 1
else
local temp=$[ $1 - 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 function8.sh
Enter a value:5
The jc of 5 is: 120


版权声明:本文为qq_41627390原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。