一、
#!/bin/sh
factorial()
{
if [ "$1" -gt "1" ]; then
i=`expr $1 - 1`
j=`factorial $i`
k=`expr $1 * $j`
echo $k
else
echo 1
fi
}
while :
do
echo "Enter a number:"
read x
factorial $x
done
二、
效果:shell实现阶乘计算
➜ ✗ bash test.sh Enter a number: 3 6 Enter a number: 4 24 Enter a number: 5 120 Enter a number:
Reference:https://www.shellscript.sh/ #Shell Scripting Tutorial