zoukankan      html  css  js  c++  java
  • Shell编程-控制结构 | 基础篇

    if-then-else分支结构

    if-then-else是一种基于条件测试结果的流程控制结构。如果测试结果为真,则执行控制结构中相应的命令列表;否则将进行另外一个条件测试或者退出该控制结构。

    if-then-else语法格式:

    if 条件1
     then 命令列表1
    elif 条件2
     then 命令列表2
    else 命令列表3
    fi

    执行逻辑流程图

    说明:当条件1成立时,则执行命令列表1并退出if-then-else控制结构;如果条件2成立,则执行命令列表2并退出if-then-else控制结构;否则执行命令列表3并退出if-then-else控制结构。在同一个if-then-else结构中只能有一条if语句和一条else语句,eilf语句可以有多条。其中if语句是必须的,elif和else语句是可选的。

    Shell脚本首先判断文件test1是否可读,如果是,则输出 is readable !的提示信息;否则不进行任何动作。

    [root@localhost 20190105]# vi test.sh 
    filename=test1
    if [ -r $filename ]            //输出test1可读则输出信息
    then
    echo $filename' is readable !'
    fi
    [root@localhost 20190105]# sh test.sh 
    test1 is readable !

    Shell脚本会判断number变量是否等于100,如果是,则输出 The number is equal 100 !的提示;否则输出 The number is not equal 100 !。

    [root@localhost 20190105]# vi number.sh
    number=200
    if [ $number -eq 100 ]                 //如果number等于100则输出“The number is equal 100 !”提示
    then
           echo 'The number is equal 100 !'
    else                            //否则输出“The number is not equal 100 !”提示
           echo 'The number is not equal 100 !'
    fi
    [root@localhost 20190105]# sh number.sh 
    The number is not equal 100 !

    Shell脚本首先判断number变量是否小于10,如果是则输出 The number < 10 !;否则,判断number变量是否大于等于10且小于20。如果是则输出 10 =< The number < 20 !;否则,判断 number变量是否大于等于20且小于30。如果是,则输出 20 =< The number < 30 !;否则,输出 30 <= The number !。

    [root@localhost 20190105]# vi number1.sh
    number=25
    if [ $number -lt 10 ]              //如果number小于10
    then
           echo 'The number < 10 !'
    elif [ $number -ge 10 -a $number -lt 20 ] //如果number大于等于10且小于20
    then
           echo '10 =< The number < 20 !'
    elif [ $number -ge 20 -a $number -lt 30 ] //如果number大于等于20且小于30
    then
           echo '20 =< The number < 30 !'
    else                         //除上述3种情况以外的其他情况
           echo '30 <= The number !'
    fi
    [root@localhost 20190105]# sh number1.sh 
    20 =< The number < 30 !

    case分支结构

    if-then-else结构能够支持多路的分支(多个elif语句),但如果有多个分支,那么程序就会变得难以阅读。case结构提供了实现多路分支的一种更简洁的方法。

    case语法格式:

    case 值或变量 in
    模式1)
     命令列表1
     ;;
    模式2)
     命令列表2
     ;;
    ...
    esac

    case语句后是需要进行测试的值或者变量。Shell会顺序地把需要测试的值或变量与case结构中指定的模式逐一进行比较,当匹配成功时,则执行该模式相应的命令列表并退出case结构(每个命令列表以两个分号“;;”作为结束)。如果没有发现匹配的模式,则会在esac后退出case结构。

    如下该脚本对number变量的值进行测试,与模式匹配的话,则输出相应的信息。

    [root@localhost 20190105]# vi case.sh
    number=66
    case $number in
    33) echo 'The number is 33 !'       //number 变量等于 33
    ;;
    44) echo 'The number is 44 !'       //number 变量等于 44
    ;;
    55) echo 'The number is 55 !'       //number 变量等于 55
    ;;
    66) echo 'The number is 66 !'       //number 变量等于 66
    ;;
    77) echo 'The number is 77 !'       //number 变量等于 77
    ;;
    88) echo 'The number is 88 !'       //number 变量等于 88
    ;;
    esac                            //结束 case 结构
    [root@localhost 20190105]# sh case.sh 
    The number is 66 !                   //命令的输出结果

    for循环结构

    for循环结构可以重复执行一个命令列表,基于for语句中所指定的值列表决定是继续循环还是跳出循环。for循环在执行命令列表前会先检查值列表中是否还有未被使用的值,如有的话,则把该值赋给for语句中指定的变量,然后执行循环结构中的命令列表。如此循环,直到值列表中的所有值都被使用。

    for循环结构语法:

    for 变量名 in 值列表
    do
     命令1
     命令2
     命令3
     ...
    done
    • 以常量作为值列表

    使用变量1、2、3、4、5、6作为值列表,for循环中只是简单的把值列表中的值进行输出。

    [root@localhost 20190105]# vi for1.sh
    #!/bin/bash
    for n in 1 2 3 4 5 6      //循环读取 1-6
    do
           echo $n
    done
    由运行结果可以非常清楚的了解for循环的运行过程。
    [root@localhost 20190105]# sh for1.sh 
    1
    2
    3
    4
    5
    6
    • 以变量作为值列表

    值列表可以是一个环境变量。

    [root@localhost 20190105]# vi for2.sh
    #!/bin/bash
    values="1 2 3 4 5 6"         //对 values 变量赋值
    for n in $values            //循环读取 values 变量中的值
    do
           echo $n
    done
    [root@localhost 20190105]# sh for2.sh 
    1
    2
    3
    4
    5
    6
    • 以命令运行结果作为值列表

    Shell支持使用命令的运行结果作为for循环的值列表。在Shell中通过"`命令`"或者“$(命令)”来引用命令的运行结果。将会以ls命令的结果作为值列表。

    [root@localhost 20190105]# vi for3.sh
    #!/bin/bash
    for n in `ls`         //循环读取 ls 命令的输出结果
    do
           echo $n      //输出变量 n 的值
    done
    [root@localhost 20190105]# sh for3.sh 
    case.sh
    for1.sh
    for2.sh
    for3.sh
    HelloWorld.sh
    number1.sh
    number.sh
    test1
    test2
    test.sh

    expr命令计算器

    expr是一个命令行的计数器,用于加、减、乘、除运算。

    [root@localhost 20190105]# expr 123 + 456 - 78  //123 加 456 减 78 等于 501
    501
    [root@localhost 20190105]# expr 9 * 8       //9 乘以 8 等于 72
    72
    [root@localhost 20190105]# expr 666 / 8      // 666 除以 8 等于 83
    83

    在循环结构中,expr 会被用作增量计算,初始值为10,每次使用expr增加加11/12。注意:这里使用expr命令时都使用的是反撇号,不是单引号。

    [root@localhost 20190105]# number=10
    [root@localhost 20190105]# number=`expr $number + 11` //对number变量的值加11
    [root@localhost 20190105]# echo $number
    21
    [root@localhost 20190105]# number=`expr $number + 12` //对number变量的值加12
    [root@localhost 20190105]# echo $number
    33

    while循环结构

    while结构会循环执行一系列的命令,并基于while语句中所指定的测试条件决定是继续循环还是跳出循环。如果条件为真,则while循环会执行结构中的一系列命令。命令执行完毕后,控制返回循环顶部,从头开始重新执行直到测试条件为假。

    while循环结构的语法:

    while 条件
    do
     命令1
     命令2
     ...
    done
    • 循环增量计算:是在while循环中使用增量计算,其运行结果如下。

    [root@localhost 20190105]# vi while1.sh 
    #!/bin/bash
    count=0             //将 count 变量置 0 
    #当 count 变量小于5时继续循环
    while [ $count -lt 5 ]
    do
    #每循环一次,count 变量的值加1
           count=`expr $count + 1`
           echo $count
    done
    [root@localhost 20190105]# sh while1.sh 
    1
    2
    3
    4
    5
    [root@localhost 20190105]#
    • 循环从文件中读取内容

    现有一文件,保存了学生的成绩信息,其中第一列是学生名,第二列是学生的成绩。

    [root@localhost 20190105]# vi students.log 
    jake 85
    tom  68
    lucy 79
    sam  95

    现在要对以上文件中的学生成绩进行统计,计算学生的数量以及学生的平均成绩。通过 while read 语句读取变量 STUDENT 和 SCORE 的内容,然后在 while 循环中通过 expr 命令计算学生总数和学生总成绩,最后计算平均值并输出。执行该脚本时需要把 students.log 文件的内容重定向到 while2.sh脚本中。

    [root@localhost 20190105]# vi while2.sh 
    #!/bin/bash
    TOTAL=0            //将变量 TOTAL 置 0
    COUNT=0            //将变量 COUNT 置 0
    #循环读取数据
    while read STUDENT SCORE
    do
    #计算总成绩
           TOTAL=`expr $TOTAL + $SCORE`
    #计算学生数
           COUNT=`expr $COUNT + 1`
    done
    #计算平均成绩
    AVG=`expr $TOTAL / $COUNT`
    echo 'There are '$COUNT' students , the avg score is '$AVG
    [root@localhost 20190105]# sh while2.sh < students.log 
    There are 4 students , the avg score is 81
    [root@localhost 20190105]#

    until循环结构

    until是除 for 和 while以外的一种循环结构,它会循环执行一系列命令直到条件为真时停止。

    until循环结构语法:

    until 条件
    do
     命令1
     命令2
     ...
    done

    until循环中读取用户输入的内容并显示到屏幕上,当用户输入的内容为 exit 时结束循环。

    [root@localhost 20190105]# vi until1.sh 
    #!/bin/bash
    xxx=""
    #当 ans 变量的值为 exit 时结束循环
    until [ "$xxx" = exit ]
    do
    #读取用户的输入到ans变量
           read xxx
    #如果用户输入的不是 exit 则输出用户的输入
           if [ "$xxx" != exit ]
           then
                   echo 'The user input is : '$xxx
    #否则退出循环
           else
                   echo 'Exit the script.'
           fi
    done
    [root@localhost 20190105]# sh until1.sh 
    hello
    The user input is : hello
    welcome to HongKong!
    The user input is : welcome to HongKong!
    exit
    Exit the script.
    [root@localhost 20190105]#
  • 相关阅读:
    分层开发的优势
    分层开发的特点
    三层开发遵循的原则
    为什么需要分层
    什么是JNDI
    为什么需要JavaBean
    连接池中的连接对象是由谁创建的呢?
    什么是连接池技术
    为什么使用连接池?(为什么要使用JNDI)
    Servlet加载
  • 原文地址:https://www.cnblogs.com/jacktian-it/p/10607054.html
Copyright © 2011-2022 走看看