该程序假设接受用户输入的号码并打印出那么多质数.由于某种原因,该程序无法正常工作.我是bash脚本的新手,这是我的第一个程序.在我看来,一切似乎都是正确的.请告诉我我哪里出问题了.
This program is suppose to accept a number from the user and print that many prime numbers. For some reason the program doesn't work. I am new to bash scripting and this is my first program. To my eyes everything seems to be correct. Please tell me where I went wrong.
echo Enter num read n i=2 j=2 for(( i=2; i <= n; i++ )) do for(( j=2; j < i-1; j++ )) do if [i % j == 0]; then break fi if [i == j]; then echo " $i" fi done done这是我得到的输出
Enter num 20 prime.sh: line 12: [i: command not found prime.sh: line 18: [i: command not found prime.sh: line 12: [i: command not found prime.sh: line 18: [i: command not found . . .进行建议的更改后
read -p "Enter a number : " n i=2 j=2 for(( i=2; i <= n; i++ )) do for(( j=2; j <= i-1; j++ )) do if [ $(( i % j )) == 0 ] then break fi if [ i == j ] then echo " $i" fi done done我能够运行该程序,但是没有产生任何结果
I was able to run the program but it didn't produce any result
i.stack.imgur/Fd1se.png
推荐答案您需要在[之后放置一个空格,因为[是应用程序.
You need to place a space after the [ because [ is an application.
并且您不能在方括号之间进行计算.您将需要告诉bash它需要计算值.因此,您需要将第11行更改为if (( i % j == 0 )).
And you can't make calculations between the brackets. You will need to tell bash it needs to calculate the values. So you would need to change line 11 to if (( i % j == 0 )).
更多推荐
Bash脚本错误:[i:找不到命令
发布评论