阶乘计算
版本一:
[root@centos17 shell]# sh -v factorial.sh #!/bin/bash factorial() { factorial1=1 for((i=1;i<=5;i++));do factorial1=$((factorial1*i)) done echo "5的阶乘是:$factorial1" } factorial 5的阶乘是:120 [root@centos17 shell]#
版本二:
[root@centos17 shell]# sh -v factorial.sh #!/bin/bash factorial() { factorial1=1 for((i=1;i<=$num;i++));do factorial1=$((factorial1*i)) done echo "$num的阶乘是:$factorial1" } num=5 factorial 5的阶乘是:120 [root@centos17 shell]#
版本三:
[root@centos17 shell]# sh -v factorial.sh 5 #!/bin/bash factorial() { factorial1=1 for((i=1;i<=$1;i++));do factorial1=$((factorial1*i)) done echo "$1的阶乘是:$factorial1" } factorial $1 5的阶乘是:120 [root@centos17 shell]#
版本四:
[root@centos17 shell]# sh -v factorial.sh 5 8 10 #!/bin/bash factorial() { factorial1=1 for((i=1;i<=$1;i++));do factorial1=$((factorial1*i)) done echo "$1的阶乘是:$factorial1" } factorial $1 5的阶乘是:120 factorial $2 8的阶乘是:40320 factorial $3 10的阶乘是:3628800 [root@centos17 shell]#
为者常成,行者常至
Give me five~!