shell脚本----for循环

 

1.方法1

 

[cpp] view plain copy
  1. #!/bin/bash  
  2.   
  3. for((i=1;i<10;i++))  
  4. do  
  5.     echo $i  
  6. done  

保存为for1.sh

 

直接sh for1.sh

会报错:

 

Syntax error: Bad for loop variable

解决方法

代码对于标准bash而言没有错,因为Ubuntu为了加快开机速度,用dash代替了传统的bash,是dash在捣鬼。
解决方法

 

(1) 取消dash
sudo dpkg-reconfigure dash
在选择项中选No,即可。

(2)chmod 777 for1.sh

直接运行 ./for1.sh

 

2.方法2

使用seq,依赖系统中有seq

 

[cpp] view plain copy
  1. #!/bin/bash  
  2. for i in `seq 10`  
  3. do  
  4.     echo $i  
  5. done  
posted @ 2018-06-19 14:32  割肉机  阅读(2011)  评论(0编辑  收藏  举报