Shell循环语句的使用(for循环、while循环、until循环)

2023-12-05 0 997
目录
  • 一.for循环
    • 1.基本格式
    • 2.脚本小仓库
  • 二.while循环
    • 1.基本格式
    • 2.脚本小仓库
  • 三.until循环
    • 1.基本格式
    • 2.脚本小仓库
  • 四.嵌套循环
    • 五.循环语句中的break、exit和continue的使用
      • 1.break的使用
      • 2.exit的使用
      • 3.continue的使用

    一.for循环

    1.基本格式

    读取不同的变量值,用来逐个执行同一组命令,经常使用在已经知道要进行多少次循环的场景。

    格式:
    for 变量名 in 取值列表
    do
    命令序列
    done

    2.脚本小仓库

    2.1 打印一列问号

    #!/bin/bash
    for i in {1..9}
    do
    echo -e \” ? \”
    done

    Shell循环语句的使用(for循环、while循环、until循环)

    2.2 打印正方形

    #!/bin/bash
    for j in {1..9}
    do
    for i in {1..9}
    do
    echo -e \” * \\c\”
    #\\c换行
    done
    echo
    #换行
    done

    Shell循环语句的使用(for循环、while循环、until循环)

    2.3 九九乘法表

    #!/bin/bash
    for j in {1..9}
    do
    for i in `seq $j`
    do
    echo -e \”${i}x${j}=$[i*j] \\t\\c\”
    #\\t tab键可以对齐
    done
    echo
    done

    Shell循环语句的使用(for循环、while循环、until循环)

    2.4 求1..10奇数和

    #/bin/bash
    sum=0
    for i in {1..10..2}
    do
    sum=$[sum+i]
    let i++
    done
    echo \”10以内的奇数和为:$sum\”

    Shell循环语句的使用(for循环、while循环、until循环)

    2.5批量创建用户并修改密码

    #!/bin/bash
    for user in {1..10}
    do
    echo stu${user}|xargs -n1 useradd
    echo \”123123\”|passwd –stdin stu${user}
    done

    Shell循环语句的使用(for循环、while循环、until循环)

    2.6 1累加到100方法一

    #!/bin/bash
    sum=0
    for ((i=0;i<=100;i++))
    do
    sum=$[i+sum]
    done
    echo sum=$sum

    2.7 1累加到100方法二

    #!/bin/bash
    sum=0
    for i in {1..100}
    do
    let sum=$i+$sum
    #sum=$[i+sum]
    #两种方法都可以
    done
    echo sum=$sum

    二.while循环

    1.基本格式

    重复测试某个条件,只要条件成立则反复执行

    格式:
    while [ 条件测试操作 ]
    do #do代表循环的开始
    判断式/命令序列
    done #done代表循环的结束

    2.脚本小仓库

    2.1 猜价格小游戏

    #!/bin/bash
    p=`echo $[RANDOM%1000+1]`
    time=0
    while true
    do
    let time++
    read -p \”请输入您猜测的价格(1-1000):\” h
    if [ $h -eq $p ]
    then
    echo \”恭喜您猜中了,您一共猜测了$time次\”
    exit
    elif [ $h -gt $p ]
    then
    echo \”您猜测的价格过高\”
    else
    echo \”您猜测的价格过低\”
    fi
    done

    2.2 1累加到100方法三

    #!/bin/bash
    i=0
    sum=0
    while [ $i -le 100 ]
    do
    sum=$[i+sum]
    let i++
    done
    echo $sum

    三.until循环

    1.基本格式

    重复测试某个条件,只要条件不成立则反复执行

    格式:
    until [ 条件测试操作 ]
    do
    判断式/命令序列
    done

    2.脚本小仓库

    2.1 1累加到100方法四

    #!/bin/bash
    sum=0
    i=0
    until [ $i -gt 100 ]
    do
    sum=$[sum+i]
    let i++
    done
    echo \”{1..100}的和:$sum\”

    四.嵌套循环

    在循环内部再使用一个循环称为嵌套循环

    格式:
    #!/bin/bash
    for ((i=1;i<5;i++))
    do
    echo 此${i}为外部循环
    for((j=1;j<4;j++))
    do
    echo -e \”\\t此${j}为内部循环\”
    done
    done

    Shell循环语句的使用(for循环、while循环、until循环)

    五.循环语句中的break、exit和continue的使用

    1.break的使用

    当满足条件的情况下break会跳出当前循环体

    #!/bin/bash
    for ((i=1;i<5;i++))
    do
    echo 此${i}为外部循环
    for((j=1;j<4;j++))
    do
    if [ $j -eq 3 ] <——如果j的值为3
    then
    break <——跳出当前循环(内部)
    fi
    echo -e \”\\t此${j}为内部循环\”
    done
    done

    Shell循环语句的使用(for循环、while循环、until循环)

    2.exit的使用

    当满足条件的情况下exit会直接退出当前脚本

    #!/bin/bash
    for ((i=1;i<5;i++))
    do
    echo 此${i}为外部循环
    for((j=1;j<4;j++))
    do
    if [ $j -eq 3 ] <——如果j的值为3
    then
    exit <——结束当前脚本
    fi
    echo -e \”\\t此${j}为内部循环\”
    done
    done

    3.continue的使用

    continue中止某次循环中的命令,但不会完全中止整个命令

    #!/bin/bash
    for ((i=1;i<5;i++))
    do
    echo 此${i}为外部循环
    for((j=1;j<4;j++))
    do
    if [ $j -eq 2 ] <——如果j的值为2
    then
    continue <——中止循环中本次的命令,但不会完全中止整个循环或脚本
    fi
    echo -e \”\\t此${j}为内部循环\”
    done
    done

    到此这篇关于Shell循环语句的使用(for循环、while循环、until循环)的文章就介绍到这了,更多相关Shell循环语句内容请搜索悠久资源网以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠久资源网!

    您可能感兴趣的文章:

    • Shell循环语句及中断语句的使用
    • shell编程中for循环语句的实现过程及案例
    • shell脚本实战-while循环语句
    • Shell脚本的条件控制和循环语句
    • shell脚本编程之循环语句
    • shell脚本编程之循环语句学习笔记
    • shell中的循环语句、判断语句实例
    • Shell脚本while、until循环语句简明教程
    • Shell脚本for循环语句简明教程
    • Shell中的循环语句for、while、until实例讲解
    • shell基础学习中的字符串操作、for循环语句示例

    收藏 (0) 打赏

    感谢您的支持,我会继续努力的!

    打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
    点赞 (0)

    悠久资源 linux shell Shell循环语句的使用(for循环、while循环、until循环) https://www.u-9.cn/jiaoben/linuxshell/101804.html

    常见问题

    相关文章

    发表评论
    暂无评论
    官方客服团队

    为您解决烦忧 - 24小时在线 专业服务