首页 > 代码库 > 循环语句

循环语句

1.while循环

while语法:
http://codingstandards.iteye.com/blog/780524
===================输出1到20
[root@lvm ~]# cat while.sh
#!/bin/bash
i=1
while [ $i -le 20 ]
do
echo $i
let i++


done
===

================= 累加
[root@lvm ~]# cat while.sh
i=1
while ((i<=20))
do
touch $i
echo $i file create ok
let i++
done
===

======================死循环
[root@lvm ~]# cat while.sh
#!/bin/bash
i=1
while [ 1 ];do
echo $i
let i++
done
===

======================同上
#!/bin/bash
i=1
while file /etc/hosts ;do #file /etc/hosts 只是一个判断条件
echo $i
let i++
done


2.for循环

for语法
==========================输出1到$num
#!/bin/bash
#for i in {1..20}
read -p "please key in number:" num
for i in `seq $num`
do
echo $i
done
===

=====================================
###for循环给循环赋初始值
for i in `seq 1 1000000`;do
echo $i
done
======================================

 

循环语句