shell实现阶乘


#!/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实现阶乘)