while和for一样都是循环语句,相对来说,for循环用的会比较多一点。但是while循环在读取文件行的时候会特别好用。
while 格式
- while [ 条件 ]
- do
- 内容
- done
实例:判断i是否小与等于5,如果小于等于5则打印i的值
- i=1
- while [ $i -le 5 ]
- do
- echo $i
- i=$(($i+1))
- done
[root@XiaoPeng scripts]# bash while.sh
1
2
3
4
5
利用while读取文件行数
- #!/bin/bash -
- while read line
- do
- echo $line
- done<a.txt
[root@XiaoPeng scripts]# bash whileline.sh
lll
222
333
444
555
666
版权所有:arppinging