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  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM