- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
Linux Shell编程 函数 函数定义 在shell中还可以定义函数。函数实际上也是由若干条shell命令组成的,因此它与shell程序形式上是相似的,不同的是它不是一个单独的进程,而是shell程序的一部分。函数定义的基本格式为: function fname or fname { 若干命令行 } 调用函数的格式为: fname param1 param2…… shell函数可以完成某些例行的工作,而且还可以有自己的退出状态,因此函数也可以作为if、while等控制结构的条件。 在函数定义时不用带参数说明,但在调用函数时可以带有参数,此时shell将把这些参数分别赋予相应的位置参数$1、$2、...及$*。 #!/bin/bashfunction fun1 {? echo? this is the first function}fun1fun2() {? echo? this is the second function}fun2 #bash test.sh this is the first functionthis is the second function 必须先定义,再调用 #!/bin/bashfunction fun1 {? echo? this is the first function}fun1fun2fun2() {? echo? this is the second function} 函数名必须唯一 #!/bin/bashfunction fun1 {? echo? this is the first function}fun1fun1() {? echo? this is the second function}fun1#bash test.sh this is the first functionthis is the second function #!/bin/bash function hello () {??echo Hello,$1 today is `date` }?echo now going to the function hello? hello kmust echo back from the function? #!/bin/bashfunction fun1 {? echo? this is the first function? ls -l xx}fun1echo the exit status is :$? #bash test.sh this is the first functionls: xx: 没有那个文件或目录the exit status is :2 函数的退出状态为2,因为最后一条命令执行出错 #!/bin/bashfunction fun1 {? ls -l xx? echo? this is the first function}fun1echo the exit status is :$? #bash test.sh ls: xx: 没有那个文件或目录this is the first functionthe exit status is :0 退出状态值却是0 ,因为最后一条命令执行无错 return命令可以使用单个整数值来定义函数退出状态 #!/bin/bashfunction fun1 {? read -p enter a value: value? echo doubling the value? return $[ $value * 2 ]}fun1echo the new value is $? #bash test.sh enter a value:24doubling the valuethe new value is 48 #!/bin/bashfname () {?read -p “please input a value:” value?echo $[ $value * 2 ]}result=`fname`#反引号echo the result is : $result#bash test.sh please input a value:13the result is : 26 函数参数 #!/bin/bashfunction fname {?if [ $# -eq 0 ] || [ $# -gt 2 ]?then?? echo -1?elif [ $# -eq 1 ]?then?? echo $[ $1 +
文档评论(0)