• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > Shell脚本函数定义和函数参数

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

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了shell脚本函数,shell脚本的函数用法,shell脚本中的函数,shell脚本调用函数,shell脚本定义变量等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

一、Shell函数

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

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

shell中函数的定义格式如下:
    command1
    command2
    ...
    commandN
    [ return value ]
}</div>

如果愿意,可在函数名前加上关键字function,这取决于使用者。
    command1
    command2
    ...
    commandN
    [ return value ]
}</div>

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

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

下面的例子定义了一个函数并进行调用:
#!/bin/bash
demoFun(){
    echo "This is your first shell function!"
}
echo "Function begin..."
hello
echo "Function end!"
</div>

输出:
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 $? !"
</div>

输出类似下面:
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
</div>

输出:
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}来获取参数。

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

</div>

您可能想查找下面的文章:

  • Shell脚本中使用function(函数)示例
  • Shell脚本函数定义和函数参数
  • shell脚本函数:控制颜色、定位、居中显示的代码

相关文章

  • Shell脚本实现硬盘空间和表空间的使用情况统计并邮件通知
  • Shell编程中while与for的区别及用法详解
  • linux whatis与whatis database 使用及查询方法(man使用实例)
  • 从apnic提取ip信息脚本分享
  • Linux中获取某个进程的系统调用以及参数(故障排查案例)
  • 分享个简易版Linux服务器初始化Shell脚本
  • shell自定义函数及参数调用解析
  • mysql源码安装脚本分享
  • 自动生成linux网卡配置脚本分享
  • 用git打更新包的办法分享

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • linux rsync同步命令(值得收藏)
    • csh,tcsh,bash,sh等shell的区别
    • 从apnic提取ip信息脚本分享
    • git 使用及常用命令
    • linux中kvm的安装及快照管理
    • 大小写字母转换的shell脚本代码
    • shell常用重定向实例讲解
    • Ubuntu、Linux Mint一键安装Chrome浏览器的Shell脚本分享
    • 判断输入的日期是否正确的shell脚本
    • linux启动级别的含义(init 0-6)

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有