美文网首页
shell实现阶乘

shell实现阶乘

作者: tmax | 来源:发表于2018-07-07 00:46 被阅读0次

    #!/bin/shell
    #阶乘
    expr $1 + 2 >& /dev/null
    flag=$?
    re=1
    if [ $flag -ne 0 ]
    then
            echo "参数错误!"
    elif [ $1 -le 0 ]
    then
            echo "1"
    else
            for((i=1;i<=$1;i=i+1))
            do
                    re=$[re*i]
            done
            echo "$re"
    fi
    

    效果

    ptmax@ubuntu:~/shell$ bash ./jiecheng_ok 3
    6
    ptmax@ubuntu:~/shell$ bash ./jiecheng_ok 5
    120
    

    相关文章

      网友评论

          本文标题:shell实现阶乘

          本文链接:https://www.haomeiwen.com/subject/tbeiuftx.html