一、

#!/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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-30
  • 2021-08-23
  • 2021-08-05
  • 2021-09-04
  • 2021-11-18
猜你喜欢
  • 2022-12-23
  • 2021-08-11
  • 2021-07-25
  • 2022-12-23
  • 2022-02-25
  • 2022-12-23
  • 2021-08-28
相关资源
相似解决方案