linux shell之使用局部变量的递归

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

linux shell之使用局部变量的递归

上一篇:Shell 08 awk高级用法


下一篇:shell使用read命令来接受输入