为什么在sh中找到factorial的代码不起作用?

时间:2017-05-03 13:49:44

标签: sh

    echo " enter the number "
read a
fact = 1
while [ $a -gt 1 ]

    do
        fact = ` expr $fact \* $a ` 
        a = ` expr $a - 1 `
        done
        echo " factorial of given number is $fact"

我想找到一个数字的阶乘。 显示的输出是

expr: syntax error
abc: 7: abc: fact: not found
abc: 8: abc: a: not found

1 个答案:

答案 0 :(得分:2)

=之前或之后的作业cannot not have spaces,否则它们被解释为命令或不指定任何值:

echo " enter the number "
read a
fact=1
while [ $a -gt 1 ]
do
  fact=`expr $fact \* $a`
  a=`expr $a - 1 `
done
echo " factorial of given number is $fact"