循环往往使用两种,for循环和while循环。
(1)for循环
#!/bin/bash # for循环的使用方式 # 方式一 for i in $(seq 10) do echo "hello$i" done # 方式二 for j in a b c d do echo "world$j" done # 方式三 for ((k=1; $k<=10; k++)) do echo "create database analysis_$k" sleep 1 done
(2)while循环
#!/bin/bash # while循环的使用方式 # 方式一 i=1 while [ $i -le 10 ] do echo "while$i" let i++ done echo "------------------------------" # 方式二 k=1 while((k<=10)) do echo "while$k" let k++ done(3)while死循环
#!/bin/bash # while死循环 while [ 1 ] do echo "循环ing" sleep 2 done(4)循环案例
#!/bin/bash # 循环案例 # 循环遍历myfolder目录下的所有html文件,并打印文件名(不带后缀) cd /home/liuzhiwei/shell-test/myfolder if [ $? -eq 0 ];then for my_file in $(ls *.html) do echo $(basename $my_file .html) done fi