echo A program to print primes between 2 numbers
read -p "Enter starting number of range: " n1
read -p "Enter ending number of range: " n2
echo ""
if [ $n1 -le $n2 ]
then
while [ $n1 -le $n2 ]
do
d=2
while [ $d -lt $n1 ]
do
if [ $((n1%d)) -eq 0 ]
then
break
fi
if [ $d -eq $n1 ]
then
echo Prime: $d
fi
d=$((d+1))
done
n1=$((n1+1))
done
else
echo First number must be smaller than second number
exit
fi
答案1
如果你正确地缩进了程序,错误会更明显。
代码
if [ $d -eq $n1 ]
then
echo Prime: $d
fi
是在while [ $d -lt $n1 ]
循环内部,因此永远不会执行,因为d=$((d+1))
is 位于该循环的最后。