Bash脚本错误:[i:找不到命令 [英] Bash script error: [i: command not found

查看:584
本文介绍了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

http://i.stack.imgur.com/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:找不到命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆