Shell脚本函数定义和函数参数

所属分类: 脚本专栏 / linux shell 阅读数: 1437
收藏 0 赞 0 分享

一、Shell函数

本教程目前为止所有脚本都是从头到尾执行。这样做很好,但你也许已经注意到有些脚本段间互相重复。

shell允许将一组命令集或语句形成一个可用块,这些块称为shell函数。

shell中函数的定义格式如下:

复制代码 代码如下:
函数名(){
    command1
    command2
    ...
    commandN
    [ return value ]
}

如果愿意,可在函数名前加上关键字function,这取决于使用者。

复制代码 代码如下:
function 函数名(){
    command1
    command2
    ...
    commandN
    [ return value ]
}

函数返回值,可以显示增加return语句;如果不加,则将最后一条命令运行结果作为返回值(一般为0,如果执行失败则返回错误代码)。 return后跟数值(0-255)。

函数可以放在同一个文件中作为一段代码,也可以放在只包含函数的单独文件中。函数不必包含很多语句或命令,甚至可以只包含一个echo语句,这取决于使用者。

下面的例子定义了一个函数并进行调用:

复制代码 代码如下:

#!/bin/bash
demoFun(){
    echo "This is your first shell function!"
}
echo "Function begin..."
hello
echo "Function end!"

输出:
Function begin...
This is your first shell function!
Function end!

下面定义一个带有return语句的函数:

复制代码 代码如下:

#!/bin/bash
funWithReturn(){
    echo "The function is to get the sum of two numbers..."
    echo -n "Input first number: "
    read aNum
    echo -n "Input another number: "
    read anotherNum
    echo "The two numbers are $aNum and $anotherNum !"
    return $(($aNum+$anotherNum))
}
funWithReturn
echo "The sum of two numbers is $? !"

输出类似下面:
The function is to get the sum of two numbers...
Input first number: 25
Input another number: 50
The two numbers are 25 and 50 !
The sum of two numbers is 75 !

函数返回值在调用该函数后通过 $? 来获得。

注意:所有函数在使用前必须定义。这意味着必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用。调用函数仅使用其函数名即可。

二、Shell函数参数

在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...

带参数的函数示例:

复制代码 代码如下:

#!/bin/bash
funWithParam(){
    echo "The value of the first parameter is $1 !"
    echo "The value of the second parameter is $2 !"
    echo "The value of the tenth parameter is $10 !"
    echo "The value of the tenth parameter is ${10} !"
    echo "The value of the eleventh parameter is ${11} !"
    echo "The amount of the parameters is $# !"
    echo "The string of the parameters is $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

输出:
The value of the first parameter is 1 !
The value of the second parameter is 2 !
The value of the tenth parameter is 10 !
The value of the tenth parameter is 34 !
The value of the eleventh parameter is 73 !
The amount of the parameters is 12 !
The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !"

注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。

另外,还有几个特殊字符用来处理参数:

更多精彩内容其他人还在看

php编译安装常见错误大全和解决方法

这篇文章主要介绍了php编译安装常见错误大全和解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Linux base shell重定向详解

这篇文章主要介绍了Linux base shell重定向的相关资料,并用一个简明例子总结了常见用法(在第三节),需要的朋友可以参考下
收藏 0 赞 0 分享

Linux Shell 常见的命令行格式简明总结

这篇文章主要介绍了Linux Shell 常见的命令行格式简明总结,非常实用,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell 命令替换的两种方式

这篇文章主要介绍了Shell 命令替换的两种方式,需要的朋友可以参考下
收藏 0 赞 0 分享

Python创建、删除桌面、启动组快捷方式的例子分享

这篇文章主要介绍了Python创建、删除桌面、启动组快捷方式的例子分享,需要的朋友可以参考下
收藏 0 赞 0 分享

shell基础学习中的字符串操作、for循环语句示例

这篇文章主要介绍了shell基础学习中的字符串操作、for循环语句示例
收藏 0 赞 0 分享

shell脚本中28个特殊字符的作用简明总结

这篇文章主要介绍了shell脚本中28个特殊字符的作用简明总结,需要的朋友可以参考下
收藏 0 赞 0 分享

linux shell流程控制语句实例讲解(if、for、while、case语句实例)

linux shell有一套自己的流程控制语句,其中包括条件语句(if),循环语句(for,while),选择语句(case)。下面我将通过例子介绍下,各个语句使用方法
收藏 0 赞 0 分享

分享一个实用的iptables脚本(各种过滤写法参考)

这篇文章主要介绍了分享一个实用的iptables脚本(各种过滤写法参考),需要的朋友可以参考下
收藏 0 赞 0 分享

shell脚本实现ssh自动登录功能分享

mac下没有找到好用的类似secureCRT,就自己写了个自动登录的脚本,分享一下,如果是新浪的,就基本不用修改代码就直接能用
收藏 0 赞 0 分享
查看更多