• SHELL学习笔记二


    SHELL学习笔记一

    SHELL学习笔记二

    SHELL学习笔记三

     使用 if-then 语句
     嵌套 if 语句  
     test 命令
     复合条件测试
     使用双方括号和双括号
     case 命令

     使用 if-then 语句

    if command
    then
        commands
    fi 
    $ cat test1.sh 
    #!/bin/bash 
    # testing the if statement 
    if pwd 
    then 
        echo "It worked" 
    fi 
    $ 
    
    $ ./test1.sh 
    /home/Christine 
    It worked 
    $ 

     if-then-else 语句

    if command 
    then 
       commands 
    else 
       commands 
    fi 
    $ cp test3.sh test4.sh 
    $ 
    $ nano test4.sh 
    $ 
    $ cat test4.sh 
    #!/bin/bash 
    # testing the else section 
    # 
    testuser=NoSuchUser 
    # 
    if grep $testuser /etc/passwd 
    then 
       echo "The bash files for user $testuser are:" 
       ls -a /home/$testuser/.b* 
       echo 
    else 
       echo "The user $testuser does not exist on this system." 
       echo 
    fi 
    $ 
    $ ./test4.sh 
    The user NoSuchUser does not exist on this system. 
    $ 

    嵌套 if 

    if command1 
    then 
       commands 
    elif command2 
    then 
        more commands 
    fi 
    $ cat test5.sh 
    #!/bin/bash 
    # Testing nested ifs - use elif 
    # 
    testuser=NoSuchUser 
    # 
    
    if grep $testuser /etc/passwd 
    then 
       echo "The user $testuser exists on this system." 
    # 
    elif ls -d /home/$testuser 
    then 
       echo "The user $testuser does not exist on this system." 
       echo "However, $testuser has a directory." 
    # 
    fi 
    $ 
    $ ./test5.sh 
    /home/NoSuchUser 
    The user NoSuchUser does not exist on this system. 
    However, NoSuchUser has a directory. 
    $ 

     test 命令

    if test condition 
    then 
       commands 
    fi 
    $ cat test6.sh 
    #!/bin/bash 
    # Testing the test command 
    # 
    if test 
    then 
       echo "No expression returns a True" 
    else 
       echo "No expression returns a False" 
    fi 
    $ 
    $ ./test6.sh 
    No expression returns a False 
    $ 
    #可以使用 test 命令确定变量中是否有内容。这只需要一个简单的条件表达式。 
    $ cat test6.sh 
    #!/bin/bash 
    # Testing the test command 
    # 
    my_variable="Full" 
    # 
    if test $my_variable 
    then 
       echo "The $my_variable expression returns a True" 
    # 
    else 
       echo "The $my_variable expression returns a False" 
    fi 
    $ 
    $ ./test6.sh 
    The Full expression returns a True 
    $ 
    #变量 my_variable 中包含有内容( Full ),因此当 test 命令测试条件时,返回的退出状态为 0 。这使得 then 语句块中的语句得以执行。 

      数值比较

    •  n1 -eq n2  检查n1是否与n2相等
    • n1 -ge n2  检查n1是否大于或等于n2  
    • n1 -gt n2  检查n1是否大于n2  
    • n1 -le n2  检查n1是否小于或等于n2  
    • n1 -lt n2  检查n1是否小于n2  
    • n1 -ne n2  检查n1是否不等于n2 
    $ cat numeric_test.sh 
    #!/bin/bash 
    # Using numeric test evaluations 
    # 
    value1=10 
    value2=11 
    # 
    if [ $value1 -gt 5 ] 
    then 
        echo "The test value $value1 is greater than 5" 
    fi 
    # 
    if [ $value1 -eq $value2 ] 
    then 
        echo "The values are equal" 
    else 
        echo "The values are different" 
    fi 
    # 
    $ 

    字符串比较

    • str1 = str2  检查str1是否和str2相同
    • str1 != str2  检查str1是否和str2不同
    • str1 < str2  检查str1是否比str2小
    • str1 > str2  检查str1是否比str2大
    • -n str1  检查str1的长度是否非0
    • -z str1  检查str1的长度是否为0
    #字符串不等条件也可以判断两个字符串是否有相同的值。 
    $ cat test8.sh 
    #!/bin/bash 
    # testing string equality 
    testuser=baduser 
    # 
    if [ $USER != $testuser ] 
    then 
       echo "This is not $testuser" 
    else 
       echo "Welcome $testuser" 
    fi 
    $  
    $ ./test8.sh 
    This is not baduser 
    $ 

    字符串顺序

    要测试一个字符串是否比另一个字符串大就是麻烦的开始。当要开始使用测试条件的大于或
    小于功能时,就会出现两个经常困扰shell程序员的问题:  

    •  大于号和小于号必须转义,否则shell会把它们当作重定向符号,把字符串值当作文件名;
    •  大于和小于顺序和 sort 命令所采用的不同。

    字符串大小
    -n 和 -z 可以检查一个变量是否含有数据。

    文件比较

    • -d file  检查file是否存在并是一个目录
    • -e file  检查file是否存在
    • -f file  检查file是否存在并是一个文件
    • -r file  检查file是否存在并可读
    • -s file  检查file是否存在并非空
    • -w file  检查file是否存在并可写
    • -x file  检查file是否存在并可执行
    • -O file  检查file是否存在并属当前用户所有
    • -G file  检查file是否存在并且默认组与当前用户相同
    • file1 -nt file2  检查file1是否比file2新
    • file1 -ot file2  检查file1是否比file2旧

      复合条件测试

    if-then 语句允许你使用布尔逻辑来组合测试。有两种布尔运算符可用:

    •  [ condition1 ] && [ condition2 ]
    •  [ condition1 ] || [ condition2 ] 

     if-then 的高级特性

    bash shell提供了两项可在 if-then 语句中使用的高级特性:

    •  用于数学表达式的双括号 (( expression ))
    •  用于高级字符串处理功能的双方括号 [[ expression ]]

     双括号命令符号

    • val++  后增
    • val--  后减
    • ++val  先增
    • --val  先减
    • !  逻辑求反
    • ~  位求反
    • **  幂运算
    • <<  左位移
    • >>  右位移
    • &  位布尔和
    • |  位布尔或
    • &&  逻辑和
    • ||  逻辑或
    $ cat test23.sh 
    #!/bin/bash 
    # using double parenthesis 
    # 
    val1=10 
    # 
    if (( $val1 ** 2 > 90 )) 
    then 
       (( val2 = $val1 ** 2 )) 
       echo "The square of $val1 is $val2" 
    fi 
    $  
    $ ./test23.sh 
    The square of 10 is 100 
    $ 
    $ cat test24.sh 
    #!/bin/bash 
    # using pattern matching 
    # 
    if [[ $USER == r* ]] 
    then 
       echo "Hello $USER" 
    else 
       echo "Sorry, I do not know you" 
    fi 
    $  
    $ ./test24.sh 
    Hello rich 
    $ 

    case 命令

    case variable in 
    pattern1 | pattern2) commands1;; 
    pattern3) commands2;; 
    *) default commands;; 
    esac 
    $ cat test26.sh 
    #!/bin/bash 
    # using the case command 
    # 
    case $USER in 
    rich | barbara) 
       echo "Welcome, $USER" 
       echo "Please enjoy your visit";; 
    testing) 
      echo "Special testing account";; 
    jessica) 
       echo "Do not forget to log off when you're done";; 
    *) 
       echo "Sorry, you are not allowed here";; 
    esac 
    $  
    $ ./test26.sh 
    Welcome, rich 
    Please enjoy your visit 
    $ 
  • 相关阅读:
    【个人实战】作品展播BI大屏【部分见github主页】
    JAVA设计模式之单例(singleton)
    你所不知道的redis安装方法,穿一手鞋,看一手资料
    zookeeper实现分布式锁总结,看这一篇足矣(设计模式应用实战)
    JAVA设计模式之状态模式(state)
    JAVA设计模式之适配器模式(adapter)
    JAVA设计模式之构建器模式(builder)
    Redis实现分布式锁(设计模式应用实战)
    JAVA设计模式之享元模式(flyweight)
    JAVA设计模式之组合模式(composite)
  • 原文地址:https://www.cnblogs.com/aongao/p/11731957.html
Copyright © 2020-2023  润新知