shell 函数的参数和返回值

来源:互联网 发布:c语言数组教程 编辑:程序博客网 时间:2024/05/21 19:44

  先来个例子吧!大家先想下回打印出什么“

示例1:

#!/bin/sh check(){local rc=0if [ $rc -eq 0 ];thenecho helloreturn 10fi}if check;thenecho "return is 0"elseecho "return is 201"fi

示例2:

#!/bin/sh check(){local rc=0if [ $rc -eq 0 ];thenecho hellofi}if check;thenecho "return is 0"elseecho "return is 201"fi

示例3:

#!/bin/sh check(){local rc=0if [ $rc -eq 0 ];thenecho helloreturn 0fi}if check;thenecho "return is 0"elseecho "return is 201"fi

3个例子都是对函数中返回值的判断;

示例1:答案:

helloreturn is 201

示例2:

helloreturn is 0


示例3:

helloreturn is 0


由上面的例子可以说明:当函数正常执行完毕时和返回0时,为真;返回大于0的数;为假;

这里函数返回只能返回非负整数 ;不相信的话大家可以看看;

下面来讲下函数的定义:

check(){}

或者

function check() //有的书上说()可以省略,但是有时候我测试时,不管有没有有这种写法都有错误?不知道什么原因{}
这种写法出错的原因可能和SHELL有关 也就是#!/bin/sh 有时候改成其他就没有问题,但是 第一种一直没有问题~
 
。。。。。。。。。。。。。。。。。。。。。。。。。。。待续~