[root@localhost shell]# cat -n factorial3.sh
1 #!/bin/bash
2
3 factorial()
4 {
5 local i=$1
6
7 if [ $i -eq 0 ]
8 then
9 rtn=1
10 else
11 factorial `expr $i - 1`
12 rtn=`expr $i \* $rtn ` //递归 就是rtn= $i*$rtn
13 fi
14
15 return $rtn
16 }
17
18 if [ -z $1 ]
19 then
20 echo "Need one parameter."
21 exit 1
22 fi
23
24 factorial $1
25
26 echo $rtn
阅读(872) | 评论(0) | 转发(0) |