【shell】Linux shell之while循环

前端之家收集整理的这篇文章主要介绍了【shell】Linux shell之while循环前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

while和for一样都是循环语句,相对来说,for循环用的会比较多一点。但是while循环在读取文件行的时候会特别好用。

while 格式

  1. while [ 条件 ]
  2. do
  3. 内容
  4. done

实例:判断i是否小与等于5,如果小于等于5则打印i的值

  1. i=1
  2. while [ $i -le 5 ]
  3. do
  4. echo $i
  5. i=$(($i+1))
  6. done

[root@XiaoPeng scripts]# bash while.sh
1
2
3
4
5

利用while读取文件行数

  1. #!/bin/bash -
  2. while read line
  3. do
  4. echo $line
  5. done<a.txt

[root@XiaoPeng scripts]# bash whileline.sh
lll
222
333
444
555
666

版权所有:arppinging

猜你在找的Bash相关文章