cat function13.sh
#!/bin/bash
#使用局部变量的递归
#使用递归函数实现阶乘运算
fact()
{
local num=$1
if [ "$num" -eq 0 ]
then
factorial=1
else
let "decnum=num-1"
#函数递归调用
fact $decnum
let "factorial=$num * $?"
fi
return $factorial
}
#脚本调用递归函数
fact $1
echo "Factorial of $1 is $?"
exit 0
./function13.sh 5
Factorial of 5 is 120