shell基础笔记4-循环结构

for循环结构

1、遍历/列表式循环结构

  for variable in list

  do

    statement

  done

  

1 #!/bin/sh
2 #演练for循环之遍历列表结构用法
3 for i in $(seq 3)
4 #for i in $(seq 1 3)
5     do
6         echo "output $i ..."
7     done

结果:

output 1 ...

output 2 ...

output 3 ...

(seq命令产生一个数到另一个数时间的所有整数) seq 10 20

不连续的序列:for variable in A B C E G

2、C语言风格的循环结构

当for循环的条件为真时,执行循环体中的语句,语法:

  for ((expr1;expr2;expr3))  #(()),注意

  do

    statement

  done

 1 #!/bin/sh
 2 #演练C程序式的for循环结构
 3 #提前创建一年中1月-12月所使用的数据库,将调用统计的结果插入到相应的月份中
 4 for ((month=1,month<=12,month++))
 5 do
 6     echo "create database analyse_$month..."
 7     sleep1
 8     #mysql -e "create database analyse_$month"
 9     #统计结果入相应的月份库中
10     #....
11 done

while循环结构

当while条件表达式为真时,执行循环体中的语句

  while expression

    do

      statement

    done

while循环控制的两种不同结构

  while [$i -le 10]  #需要空格

  while((i<10))  #不用跟空格

 

posted @ 2020-04-20 22:52  追逐蹉跎的岁月  阅读(122)  评论(0编辑  收藏  举报