1.用return作为函数的退出状态,然后通过echo $?来得到函数返回值,注意,return返回的值限定为0-255.

例子:

#! /bin/bash

sum()

{

   let "z=$1+$2";

  return $z;

}

sum 22 4 ; #调用sum函数。

echo "$?" ; #输出结果。

运行结果为26.

2.用$(fun $1)的形式来获取,没有限定返回值只是0-255。

#! /bin/bash

length()

{

  str=$1;

 result=0;

 if [ "$str" != "" ];then

   result=${#str} #计算字符串长度

 fi

 echo "$result";

}

len=$(length "abc123"); #调用函数

echo "the string's length is $len" #输出执行结果

结果是 the string's length is 6.

 

 

做个小推广:程序员经常久坐,颈椎毛病比较多,特别推荐ventry颈椎保健枕

posted on 2015-01-16 10:39  龙种人  阅读(495)  评论(0编辑  收藏  举报