linux-tutorial/codes/shell/示例脚本/脚本函数/函数递归.sh

17 lines
269 B
Bash
Raw Normal View History

2019-05-10 11:17:57 +08:00
#!/bin/bash
function factorial {
if [ $1 -eq 1 ]
then
echo 1
else
local temp=$[ $1 -1 ]
local result=`factorial $temp`
echo $[ $result * $1 ]
fi
}
read -p "Please input a value: " value
result=`factorial $value`
echo "The factorial of $value is: $result"