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 位於該循環的最後。