新手求助,变量前面加$问题
发表于 : 2011-12-16 10:13
# sh14.sh
# calculate the sum value of 1:n
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input the upper bound n: " n
i=1
sum=0
while [ $i != $n ]
do
sum=$ ( ($sum+$i) )
i=$ ( ($i+1) )
done
echo "The sum from 1 t o $n is $sum."
为什么while循环里,sum的求值是错的?
# calculate the sum value of 1:n
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input the upper bound n: " n
i=1
sum=0
while [ $i != $n ]
do
sum=$ ( ($sum+$i) )
i=$ ( ($i+1) )
done
echo "The sum from 1 t o $n is $sum."
为什么while循环里,sum的求值是错的?