zoukankan      html  css  js  c++  java
  • Shell学习之条件测试(四)

    Shell学习之条件测试

    目录

    逻辑测试

    文件测试

    数值比较

    字符串比较

    逻辑测试

    格式:
    
      [ 表达式 ]  操作符 [ 表达式2 ] ……
    
      命令1  操作符  命令2 ……
    
    常用的操作符 ( 注意:-a和-o放在[]里面用,&&和||放在[]外面用 )
    
        -a  或  &&           逻辑与
    
        -o  或 ||             逻辑或
    
          !               逻辑否
    

      


    文件测试

    文件测试
    
    格式1:  [  操作符 文件或目录  ]  
    
    格式2:test  操作符 文件或目录 
    
    常用的测试操作符
    
        -d :测试是否为目录( Directory )
    
        -e :测试目录或文件是否存在(Exist)
    
        -f :测试是否为文件(File)
    
        -r :测试当前用户是否可读(read)
    
        -w:测试当前用户是否可写(write)
    
        -x :测试当前用户是否可执行(excute)
    

    例子:备份Mysql数据库,业务代码没有完善

    #/bin/bash
    back_dir=/var/mysql_back
    
    if !test -d $back_dir;then
    	mkdir -p $back_dir
    fi
    echo "开始备份"
    

      

     


    数值比较

    格式1:[ 整数1 操作符 整数2 ]
    
    格式2: test 整数1 操作符 整数2
    
    常用的测试操作符
    
        -eq : 等于 (Equal)
    
        -ne : 不等于 (Not Equal)
    
        -gt : 大于(Greater Than)
    
        -lt : 小于 (Lesser Than)
    
        -le : 小于或等于(Lesser or Equal)
    
        -ge : 大于或等于(Greater or Equal)
    

      

    例子

    #/bin/bash
    if [ $UID -ne 0];then
    	echo "没有权限"
    	exit
    fi
    yum -y install httpd
    

      

    字符串比较

    格式1:[ 字符串1 = 字符串2 ]
    
         [ 字符串1 != 字符串2 ]
    
    格式2:[ -z 字符串 ]
    
    常用的测试操作符
    
      = : 字符串内容相同
    
      != : 字符串内容不同
    
      -z : 字符串内容为空
    
    

      

    例子

    #/bin/bash
    if [ $USER = "root"];then
    	yum -y install httpd
    fi
    	echo "没有权限"
    	exit
    

      

    所有表达式

    ( EXPRESSION )
                  EXPRESSION is true
    
           ! EXPRESSION
                  EXPRESSION is false
    
           EXPRESSION1 -a EXPRESSION2
                  both EXPRESSION1 and EXPRESSION2 are true
    
           EXPRESSION1 -o EXPRESSION2
                  either EXPRESSION1 or EXPRESSION2 is true
    
           -n STRING
                  the length of STRING is nonzero
    
           STRING equivalent to -n STRING
    
           -z STRING
                  the length of STRING is zero
    
           STRING1 = STRING2
                  the strings are equal
    
           STRING1 != STRING2
                  the strings are not equal
    
           INTEGER1 -eq INTEGER2
                  INTEGER1 is equal to INTEGER2
    
           INTEGER1 -ge INTEGER2
                  INTEGER1 is greater than or equal to INTEGER2
    
           INTEGER1 -gt INTEGER2
                  INTEGER1 is greater than INTEGER2
    
           INTEGER1 -le INTEGER2
                  INTEGER1 is less than or equal to INTEGER2
    
           INTEGER1 -lt INTEGER2
                  INTEGER1 is less than INTEGER2
    
           INTEGER1 -ne INTEGER2
                  INTEGER1 is not equal to INTEGER2
    
           FILE1 -ef FILE2
                  FILE1 and FILE2 have the same device and inode numbers
    
           FILE1 -nt FILE2
                  FILE1 is newer (modification date) than FILE2
    
           FILE1 -ot FILE2
                  FILE1 is older than FILE2
    
           -b FILE
                  FILE exists and is block special
    
           -c FILE
                  FILE exists and is character special
    
           -d FILE
                  FILE exists and is a directory
    
           -e FILE
                  FILE exists
    
           -f FILE
                  FILE exists and is a regular file
    
           -g FILE
                  FILE exists and is set-group-ID
    
           -G FILE
                  FILE exists and is owned by the effective group ID
    
           -h FILE
                  FILE exists and is a symbolic link (same as -L)
    
           -k FILE
                  FILE exists and has its sticky bit set
    
           -L FILE
                  FILE exists and is a symbolic link (same as -h)
    
           -O FILE
                  FILE exists and is owned by the effective user ID
    
           -p FILE
                  FILE exists and is a named pipe
    
           -r FILE
                  FILE exists and read permission is granted
    
           -s FILE
                  FILE exists and has a size greater than zero
    
           -S FILE
                  FILE exists and is a socket
    
           -t FD  file descriptor FD is opened on a terminal
    
           -u FILE
                  FILE exists and its set-user-ID bit is set
    
           -w FILE
                  FILE exists and write permission is granted
    
           -x FILE
                  FILE exists and execute (or search) permission is granted
    

      

  • 相关阅读:
    递归和回溯的区别
    N皇后问题
    c输出格式
    python sublime run快捷键设置
    八皇后问题
    动态规划---从左上角到右下角的价值最大的路径
    莫队算法详解和c实现
    Shell 常用命令总结
    WeakHashMap和HashMap的区别
    【 Jquery插件】引导用户如何操作网站功能的向导
  • 原文地址:https://www.cnblogs.com/-wenli/p/10367572.html
Copyright © 2011-2022 走看看