【发布时间】:2024-01-06 01:00:01
【问题描述】:
【问题讨论】:
-
第 7 行缺少结束反引号。
-
好的,但还是不行
-
请不要发文字图片,复制粘贴实际文字。见*.com/editing-help
-
在处理语法问题时,请考虑将您的代码(加上适当的 shebang)剪切粘贴到 shellcheck.net 并进行建议的更改
【问题讨论】:
我认为您需要在[ 之后和] 之前添加一个空格:
#!/bin/bash
echo "Enter the number to find it's factorial"
read number
total=1
counter=1
while [ $counter -le $number ];
do
total=` expr $counter \* $total`
counter=` expr $counter + 1`
done
echo $total
在 Ubuntu 上工作:
$ ./factorial.sh
Enter the number to find it's factorial
5
120
【讨论】:
[ 不仅仅是语法,它是一个命令,因此它需要空间将其与其参数分开。
while [ $counter -le $number ]
# .....^....................^
确保验证 number 实际上只是数字。
其他一些cmets:
$(...) 而不是`...` -- 请参阅
https://github.com/koalaman/shellcheck/wiki/SC2006
了解更多详情。expr。看
Arithmetic Expansion
和Shell Arithmetic
在手册中。
还有一个算术条件构造(类似于
面向字符串的[[...]] 条件构造)——见
Conditional Constructs
并向下滚动到((...))(没有直接链接)。【讨论】:
实际上,在第 7 行,您忘记了完整的反引号。
【讨论】:
-eq -lt -le -gt -ge 进行数字比较。使用= (or ==) =~ < > 进行字符串比较。