linux shell之使用局部变量的递归
时间:2021-04-10 00:28:47
收藏:0
阅读:33
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
原文:https://www.cnblogs.com/zhudaheng123/p/14638498.html
评论(0)