zoukankan      html  css  js  c++  java
  • shell编程之循环

    一、for循环

    for循环是Shelll中最常见的循环结构,根据书写习惯又分为列表for循环、不带列表的for循环以及类C的for循环。for循环是一种运行前的测试语句,也就是在运行任何循环体之前先要判断循环条件是否成立,只有在条件成立的情况下才会运行循环体,否则将会退出循环。每完成一次循环后,在进行下一次循环之前都会再次进行测试。

    1.带列表的for循环

    [root@Cfhost-170820-UCNK ~]# cat fruit01.sh
    #!/bin/bash

    fruits="apple orange banana pear"
    for FRUIT in $fruits
    do
    echo "$FRUIT is John's favorite"
    done
    echo "No more fruits"
    [root@Cfhost-170820-UCNK ~]# sh fruit01.sh
    apple is John's favorite
    orange is John's favorite
    banana is John's favorite
    pear is John's favorite
    No more fruits

    2.不带列表的for循环

    #!/bin/bash
    for VARIABLE in $@
    do
    echo -n $VARIABLE
    done

    [root@Cfhost-170820-UCNK ~]# bash for_list07.sh 1 2 3

    1 2 3

     3.类C的for循环

    计算1到100的和和以及1到100的奇数和

    [root@Cfhost-170820-UCNK ~]# cat c_for03.sh
    #!/bin/bash
    sum01=0
    sum02=0
    for (( i=1,j=1;i<=100;i++,j+=2))
    do
    let "sum01+=i"
    if [ $j -lt 100 ];then
    let "sum02+=j"
    fi
    done
    echo "sum01=$sum01"
    echo "sum02=$sum02"
    [root@Cfhost-170820-UCNK ~]# sh c_for03.sh
    sum01=5050
    sum02=2500

    4.for的无限循环

    无限循环又叫“死循环",要注意的是:和代码设计功能无关的无限循环,或者说是开发者意料之外的无限循环都属于软件bug,这类bug容易造成系统资源耗尽,造成严重的系统故障,所以要非常小心,避免出现这种问题。开发者在用循环语句的时候要尤其注意循环结束条件,有条件的要进行测试。

    死循环:

    #!/bin/bash

    for ((;1;))

    do

      echo "infinite loop"

    done

    5.while循环

    用while循环做猜字游戏

    [root@Cfhost-170820-UCNK ~]# cat while03.sh
    #!/bin/bash
    PRE_SET_NUM=8
    echo "Input a number between 1 and 10"
    while read GUESS
    do
    if [[ $GUESS -eq $PRE_SET_NUM ]];then
    echo "you get the right number"
    exit
    else
    echo "Wrong,try again"
    fi
    done

    用while按行读取文件

    [root@Cfhost-170820-UCNK ~]# cat while04.sh
    #!/bin/bash
    while read LINE

    do
    Name=`echo $LINE | awk '{print $1}'`
    AGE=`echo $LINE | awk '{print $2}'`
    Sex=`echo $LINE | awk '{print $3}'`
    echo "My name is $Name, I'm $AGE years old, I'm a $Sex"
    done < student_info.txt
    [root@Cfhost-170820-UCNK ~]# cat student_info.txt
    John 30 Boy
    Sue 28 Girl
    Wang 25 Boy
    Xu 23 Girl

    ##上述这种方式,实际应用场景是可以把参数定义在另外一个文件夹,这时如果我需要改动代码传递参数,只需改配置文件即可

    6.until循环

    until循环也是运行前测试,但是until采用的是测试假值的方式,当测试结果为假时才继续执行循环体,知道测试为真时,才停止循环

    下面使用until计算1到100的和以及1到100的奇数和

    [root@Cfhost-170820-UCNK ~]# cat until01.sh
    #!/bin/bash
    sum01=0
    sum02=0
    i=1
    until [[ $i -gt 100 ]]
    do
    let "sum01+=i"
    let "j=i%2"
    if [[ $j -ne 0 ]];then
    let "sum02+=i"
    fi
    let "i+=1"
    done
    echo $sum01
    echo $sum02

    [root@Cfhost-170820-UCNK ~]# sh until01.sh
    5050
    2500

    7.select循环

    [root@Cfhost-170820-UCNK ~]# cat select01.sh

    #!/bin/bash
    echo "which car do you prefer?"

    select CAR in Benz Audi VolksWagen

    do

    break
    done

    echo "You chose $CAR"

    [root@Cfhost-170820-UCNK ~]# sh select01.sh
    which car do you prefer?
    1) Benz
    2) Audi
    3) VolksWagen
    #? 2
    You chose Audi

    8.循环控制

    #break

    九九乘法表

    [root@Cfhost-170820-UCNK ~]# cat break01.sh
    #!/bin/bash
    for ((i=1; i<=9;i++))
    do
    for((j=1;j<=9;j++))
    do
    if [[ $j -le $i ]]; then #小于等于i时运算
    let "Multi=$i*$j"
    echo -n "$i*$j=$Multi "
    else
    break #j 一旦大于i则立即停止当前循环
    fi
    done
    echo

    [root@Cfhost-170820-UCNK ~]# sh break01.sh
    1*1=1
    2*1=2 2*2=4
    3*1=3 3*2=6 3*3=9
    4*1=4 4*2=8 4*3=12 4*4=16
    5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
    6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
    7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
    8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
    9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

    素数

    #continue

    [root@Cfhost-170820-UCNK ~]# cat continue_02.sh
    #!/bin/bash
    for ((i=1;i<=100;i++))
    do
    for ((j=2;j<i;j++))
    do
    if !(($i%$j));then
    continue 2
    fi
    done
    echo -n "$i "
    done

    [root@Cfhost-170820-UCNK ~]# sh continue_02.sh
    1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

  • 相关阅读:
    2019年4月18日 查询功能 2
    bzoj3601
    bzoj2693
    bzoj2440
    bzoj3529
    bzoj2820
    BZOJ2813
    BZOJ4515
    AtCoder Grand Contest 001 题解
    BZOJ2757
  • 原文地址:https://www.cnblogs.com/youcong/p/7933009.html
Copyright © 2011-2022 走看看