Ubuntu shell scripts报错:Syntax error: Bad for loop variable解决方法 [转]

今天晚上在实验室没事写了个简单的shell  script 如下:

 #!/bin/bash
  2 #program:
  3 #  try to calculate 1+2+3+...+[your input]
  4 #History:
  5 #2012/10/27   mupeng    First release
  6 
  7 read -p "input a number I will calctlate 1+2+..+[your input] " nu
  8 sum=0;
  9 for (( i=1; i<=$nu; i=i+1 ))
 10 do 
 11         sum=$(($sum+$i))
 12 done
 13 echo "The result of 1+2+...+$nu is ==>$sum"

结果用sh -n检查语法时居然报错:
sh14.sh: 9: Syntax error: Bad for loop variable
仔细看了又看没有发现错误啊。。。
后来终于找到原因了:
代码对于标准的bash来说没有错误,但是因为Ubuntu为了加快开机速度,用dash代替了bash,所以导致了错误。
取消dash的办法是:
sudo dpkg-reconfigure dash

在选项中选No,就可以了。

原文地址:http://blog.163.com/xh_ding/blog/static/19390328920129271006834/

猜你喜欢

转载自dawnche.iteye.com/blog/1720420