• Linux-Shell脚本编程-学习-5-Shell编程-使用结构化命令-if-then-else-elif


    if-then语句

    if-then语句格式如下

    if comman
    then
        command
    fi


    bash shell中的if语句可鞥会和我们接触的其他if语句的工作方式不同,bash shell的if语句会运行if语句后面的那个命令,如果该命令的退出状态码是0 那么执行位于then部分的的命令。

    代码实例

    #!/bin/bash
    
    #if-then test
    
    if date
    then
        echo "this is the if-then test"
    fi


    这个脚本的功能就是,执行date命令,如果date命令执行成功,那么他的退出状态码是0就执行then后面的代码,在屏幕上面输一行文字,this is the if-then test

    then后面的语句可以是一条或多条,和我们写简单的shell脚本没有区别,这里会一条一条的执行下去

    测试实例代码

    #if-then test
    
    if date
    then
        echo "this is the if-then test"
        testusername=dreamlife
        if grep $testusername /etc/passwd
            then
                echo "the bash file for user $testusername art:"
                ls -a /home/$testusername/.b*
        fi
    fi

    如果我们想要在shell和我们平常一样使用if else的功能,我们需要使用一下命令格式

    if command
    then
        command
    else
        command


    这里,如果 执行if后面的命令的退出状态码是0 就执行then后面的代码块,否则就执行else后面的代码块

    测试代码

    #!/bin/bash
    
    #if-then-else
    
    testuser=dreamlife
    if grep $testuser /etc/passwd
        then
        echo 'the files for user $testuser are:'
        ls -a /home/$testuser/ *
    else
        echo "the user name $testuser dose not exist on this system"
    fi
    


    在shell编程中,也是有if嵌套的,使用格式如下

    if command1
    then
        command1-set
    elif command2
    then 
        command-set2
    elif command3
    then
        command3-set
    
        ...
    
    fi


    这个就没有实例代码了,如果有兴趣的,可以吧上面的代码改吧改吧试试看,每次只能测试一种。

    好了,学习下一个命令,test

    test是个好东西,他的功能之一就是可以是我们shell的if可以比较真假的,test的基本命令格式很简单

    test condition

    condition是test命令要测试的一系列参数和值,当用在if-then语句的时候,test命令执行,如果tets命令中列出的条件为真的时候,退出状态码为0 否则为1,这样就可以在if-then中使用了

    就是下面的格式了

    if test condition
        then
        commands    
    fi


    不过每次这么写也挺别扭的。所以,在bash中,提供了另外一种tets的写法,那就是方括号[]

    不过必须要在左方括号右面,右方括号左面各加一个空格才可以,不然报错

    if [ condition ]
        then
        commands
    fi


    test命令可以判断三种类型条件

    1. 数值比较

    2. 字符串比较

    3. 文件比较

    第一类,test数值比较的基本功能

    1. 检查n1是否与n2相等:n1 -eq n2

    2. 检查n1是否大于或等于n2:n1 -ge n2

    3. 检查n1是否大于n2:n1 -gt n2

    4. 检查n1是否小于或等于n2:n1 -le n2

    5. 检查n1是否小于n2:n1 -lt n2

    6.检查n1是否不等于n2:n1 -ne n2

    测试用例

    #!/bin/bash
    
    #using numeric test comparisons
    
    var1=10
    var2=11
    
    if [ $var1 -gt 5 ]
        then 
        echo "the test value $var1 ia greater than 5"
    fi
    
    if [ $var1 -eq $var2 ]
        then
        echo "the values are equal"
    else
        echo "the calues are different"
    fi

    这里注意的是,浮点数不能比较。下面是错误例子
    #!/bin/bash
    
    
    #testing floating point numbers
    
    val1=`echo "scale=4;10/3" | bc`
    echo "the test values is $val1"
    if [ $val1 -gt 3 ]
        then
        echo "the result if larger than 3"
    fi

    这里bash提示需要整数表达式

    第二类 字符串的比较

    1. 检查str1是否和str2相同:str1 = str2

    2. 检查str1是否和str2不同:str1 != str2

    3. 检查str1是否比str2小:str1 <str2

    4. 检查str1是否比str2大:str1 >str2

    5. 检查str1的长度是否为非零:-n str1

    6. 检查str1的长度是否为0:-z str1

    下面是实例代码

    #!/bin/bash
    
    #testing string equality
    
    testuser=dreamlife
    
    if [ $USER = $testuser ]
        then 
        echo "Welcom $testuser"
    fi


    #!/bin/bash
    
    #testing string equality
    
    testuser=dreamlife
    
    if [ $USER != $testuser ]
        then
        echo "this user is not $testuser" 
    else
        echo "Welcom $testuser"
    fi
    在字符串比较的时候,需要注意两个问题,

    1. 大于小于符号必须使用转义,否则shell会把他们当做重定向符号而把字符串当做文件名

    2. 大于小于顺序和sort命令所采用不同

    第一个问题

    #!/bin/bash
    
    #mis-using string comparisons
    
    val1=baseball
    val2=hockey
    
    if [ $val1 > $val2 ]
        then 
        echo "$val1 is greater than $val2"
    else
        echo "$val1 is less than $val2"
    fi
    
    

    第二个儿问题
    #!/bin/bash
    
    #testing string sort order
    
    val1=testing
    val2=Testing
    
    if [ $val1 > $val2 ]
        then 
        echo "$val1 is greater than $val2"
    else
        echo "$val1 is less than $val2"
    fi

    字符串大小
    #!/bin/bash
    
    #testing string length
    
    val1=testing
    val2=''
    
    if [ -n "$val1" ]
        then 
        echo "the string '$val1' is not empty"
    else
        echo "the string '$val1' is empty"
    fi
    
    
    if [ -z "$val2" ]
        then 
        echo "the string '$val2' is empty"
    else
        echo "the string '$val2' is not empty"
    fi


    后面还有文件的比较,由于文件比较内容比较多,我会在写一个。



  • 相关阅读:
    Redis的使用
    linux面试题
    数据库面试题
    Xadmin控件的实现:〇六查询视图四——list视图search功能
    Xadmin控件的实现:〇五查询视图三——list视图封装
    Xadmin控件的实现:〇四查询视图二——分页效果的实现
    Xadmin控件的实现:〇三查询视图一——基础功能
    Xadmin控件的实现:〇项目介绍
    Xadmin控件的实现:〇二增、改视图
    离线电商数仓(十七)之用户行为数据仓库(三)高可用mysql (HA mysql,ubuntu)
  • 原文地址:https://www.cnblogs.com/DreamDog/p/9160077.html
Copyright © 2020-2023  润新知