首页 > 系统服务 > 详细

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

时间:2021-04-10 00:28:47      阅读:28      评论:0      收藏:0      [点我收藏+]

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之使用局部变量的递归

原文:https://www.cnblogs.com/zhudaheng123/p/14638498.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!