循环类型

 # 当型循环
while
    当什么,后面 [ 条件 ]
    只要条件满足成立我就循环
    循环到这个条件不满足
# 直到型循环
do_until
    后面接条件表达式
    这个条件只要不满足就一直接循环
    循环到满足为止
# do_until 很少很少 压根就不会用
# 通用型循环
for
    基本while能做for也能做
# 死循环
#!/bin/bash
while true;do
    echo '111'
    sleep 2
done
#!/bin/bash
while [ 1 -eq 1 ];do
    echo '111'
    sleep 2
done

应用场景

while
    死循环
    有条件的循环
    一行一行的读

for
## for循环能不能一行一行读?
#!/bin/bash
for domain in `cat 1.txt`;do
while循环
    echo '--------'
    echo $domain
done
## 目前执行脚本的时候确实是一行一行读了
[root@web02 test]# sh 4.sh
--------
www.yys.com
--------
blog.yys.com
--------
zh.yys.com
## 但是修改了文件内容
[root@web02 test]# cat 1.txt
www.yys.com xxx
blog.yys.com yyy
zh.yys.com zzz
## 再次执行for是按照空格来读
[root@web02 test]# sh 4.sh
--------
www.yys.com
--------
xxx
--------
blog.yys.com
--------
yyy
--------
zh.yys.com
--------
zzz

while循环

# 第一种写法
while [ 条件表达式 ];do
    cmd1
    cmd2
    cmd3
    ...
done
# 第二种
while [ 条件表达式 ]
do
    cmd1
    cmd2
    cmd3
    ...
done
#while模拟for循环seq10
#!/bin/bash
i=1
while [ $i -lt 10 ]
do
    echo $((i++))
done 
#使用while循环实现1加到100
#!/bin/bash
m=0
n=1
while [ $n -le 100 ];do
    ((m=m+n))
    ((n++))
done
echo $m
--------------------------- whiletrue使用场景多 多数使用----------------------------------------------
## while死循环
#!/bin/bash
while true;do # 条件为真
    echo 'hiehiehie'
    sleep 1
done
#!/bin/bash
while [ 1 -eq 1 ];do 1=1 # 条件为真
    echo 'hahahahaha'
    sleep 1
done
#!/bin/bash
while :;do # 纯粹的死循环
    echo 'hahahahaha'
    sleep 1
done
# for循环
#!/bin/bash
for n in `seq 10`;do
    echo $n
done
#多种计算方式
[root@web02 test]# echo {1..100} | tr ' ' '+' |bc
5050
[root@web02 test]# seq -s+ 100 | bc
5050
[root@web02 test]# seq 100 | awk '{m=m+$1}END{print m}'
5050
[root@web02 test]# awk 'BEGIN{for(i=1;i<=100;i++)sum=sum+i;print sum}'
5050

image-20230630144322699

直到型循环

until [ 条件表达式 ];do
    cmd1
    cmd2
    cmd3
    ...
done
until [ 条件表达式 ]
do
    cmd1
    cmd2
    cmd3
    ...
done
-------------------------------------------------------------------------------------------------
#!/bin/bash
n=0
until [ $n -gt 10 ];do
    echo $n
    ((n++))
done
#until 循环执行一系列命令直至条件为 true 时停止。
#until 循环与 while 循环在处理方式上刚好相反。
#一般 while 循环优于 until 循环,但在某些时候—也只是极少数情况下,until 循环更加有用。

for循环

语法1

无法指定循环次数 表达式有多少内容就循环多少次

for var in 变量表达式;do
    cmd1
    cmd2
    ...
done
for var in 变量表达式
do
    cmd1
    cmd2
    ...
done
变量表达式
- 可以是文件中的内容 (按空格循环)
- 可以是序列中命令中的内容 seq 10
- 可以是数组中的内容
- 可以是以空格为分隔符的字符串
# 以空格为分隔符
for var in xxx yyy zzz;do
    echo $var
done
[root@web02 test]# sh 5.sh
xxx
yyy
zzz

#!/bin/bash
for var in "xxx yyy zzz";do
    echo $var
done

#!/bin/bash
for var in "xxx" "yyy" "zzz";do
    echo $var
done
# 文件内容
[root@web02 test]# cat 5.sh
#!/bin/bash
for var in `cat 1.txt`;do
    echo $var
done
[root@web02 test]# sh 5.sh
www.yys.com
xxx
blog.yys.com
yyy
zh.yys.com
zzz

#!/bin/bash
for var in `cat 1.txt`;do
    echo $var
done
[root@web02 test]# sh 5.sh
www.yys.com
xxx
blog.yys.com
yyy
zh.yys.com
zzz
# 可以是序列中命令中的内容
[root@web02 test]# cat 5.sh
#!/bin/bash
for var in `seq 10`;do
    echo $var
done
[root@web02 test]# sh 5.sh
1
2
3
4
5
6
7
8
9
10
# 可以是数组中的内容
#!/bin/bash
name=(wyd hhh ljy lyf yl yx mx)
for var in ${name[*]};do
    echo $var
done
[root@web02 test]# sh 5.sh
wyd
hhh
ljy
lyf
yl
yx
mx

for循环次数

[root@web02 test]# cat 5.sh
#!/bin/bash
for((i=1;i<=10;i++));do
    echo $i
done
[root@web02 test]# sh 5.sh
1
2
3
4
5
6
7
8
9
10
# 死循环
for (( ; ; ));do
    echo 123
    sleep 1
done

循环的控制语句

continue
break

break跳出循环

break命令是允许跳出所有的循环 (终止执行后面的循环)
#!/bin/bash
while true;do
    read -p 'Please Inputh A Number:' num
    if [ $num -ne 5 ];then
        echo "你输入的是$num"
    else
        break
    fi
done
----------------------------------------------------------------------------------------------------
n=0
while [ $n -lt 10 ];do
    ((n++))
    if [ $n -eq 5 ];then
        break
    fi
    echo $n
done

continue跳出循环

while true;do
    read -p 'Please Inputh A Number:' num
    if [ $num -ne 5 ];then
        echo "你输入的是$num"
    else
        continue
    fi
done
----------------------------------------------------------------------------------------------------
#!/bin/bash
n=0
    while [ $n -lt 10 ];do
        ((n++))
    if [ $n -eq 5 ];then
        continue
    fi
    echo $n
done

while读取文件

# 第一种
#!/bin/bash
exec < 1.txt
while read line;do
    echo '---------'
    echo $line
done
[root@web02 test]# sh 5.sh
---------
"www.yys.com xxx"
---------
blog.yys.com yyy
---------
zh.yys.com zzz
# 第二种
#!/bin/bash
cat 1.txt|while read line;do
    echo '---------'
    echo $line
done
[root@web02 test]# sh 5.sh
---------
"www.yys.com xxx"
---------
blog.yys.com yyy
---------
zh.yys.com zzz
# 第三种
#!/bin/bash
while read line;do
    echo '---------'
    echo $line
done < 1.txt
[root@web02 test]# sh 5.sh
---------
"www.yys.com xxx"
---------
blog.yys.com yyy
---------
zh.yys.com zzz