Bash脚本错误:[i:找不到命令

编程入门 行业动态 更新时间:2024-10-24 12:26:07
本文介绍了Bash脚本错误:[i:找不到命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

该程序假设接受用户输入的号码并打印出那么多质数.由于某种原因,该程序无法正常工作.我是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:找不到命令

本文发布于:2023-10-13 10:29:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1487702.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   脚本   命令   错误   Bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!