求小于12的数的阶乘。
#! /bin/sh
resultFunc(){
if test $1 -ne 1;then
nextNum=$(($1-1));
# echo "nextNum=$nextNum ";
resultFunc $nextNum; //递归调用
res=$?; //保存上次调用的函数的返回值
now=$1; //保存当前函数的参数
# echo "Res=$res,Now=$now";
R=`expr $1 /* $res`;
else
R="1";
fi
return $R;
}
i=0
while test $i -ne 12
do
if test $i -eq 0;then
echo "0!=1"
else
resultFunc $i;
echo "$i!=$?";
fi
i=`expr $i + 1`
done
exit 0;
返回值不能直接赋值,需要用 $? 来获取.
return 返回的值需要是数值型数据,其他数据类型可以通过全局变量来返回或保存。