Bash script error: [i: command not found

后端 未结 4 1503
面向向阳花
面向向阳花 2021-01-22 18:03

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

4条回答
  •  -上瘾入骨i
    2021-01-22 18:41

    #!/bin/bash
    #
    # primes
    #
    read -p "Enter a  number: " n
    i=2
    
    for (( i=2; i <= n; i++ ))
    do
        for (( j=2; j*j < i; j++ ))
        do
            if ((i % j == 0))
            then
                echo "no prime, $i divider: "$j
                break
            fi
        done
    done
    

    updated, after realizing (thanks Will Ness), that all primes up to INPUT are searched.

    i needs to run to √(n) only.

提交回复
热议问题