zoukankan      html  css  js  c++  java
  • shell之if简化语句

    最常用的简化if语句:


    && 如果是“前面”,则“后面”

    [ -f /var/run/dhcpd.pid ] && rm /var/run/dhcpd.pid 检查 文件是否存在,如果存在就删掉


    || 如果不是“前面”,则后面
    [ -f /usr/sbin/dhcpd ] || exit 0 检验文件是否存在,如果存在就退出



    示例:用简化 if 和$1,$2,$3来检测参数,不合理就调用help
    [ -z "$1" ] && help 如果第一个参数不存在(-z 字符串长度为0 )

    [ "$1" = "-h" ] && help 如果第一个参数是-h,就显示help






    [macg@machome ~]$ man test
    [(1)                             User Commands                            [(1)
    
    SYNOPSIS
           test EXPRESSION
           [ EXPRESSION ]
    
    
           [-n] STRING
                  the length of STRING is nonzero          -n和直接$str都是非0条件
    
           -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 -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     文件存在且是普通文件
    
           -h FILE
                  FILE exists and is a symbolic link (same as -L)
    
           -L FILE
                  FILE exists and is a symbolic link (same as -h)
    
           -G FILE
                  FILE exists and is owned by the effective group ID
    
           -O FILE
                  FILE exists and is owned by the effective user ID
    
           -p FILE
                  FILE exists and is a named pipe
    
    
           -s FILE
                  FILE exists and has a size greater than zero
    
           -S FILE
                  FILE exists and is a socket
    
           -w FILE
                  FILE exists and is writable
    
           -x FILE
    FILE exists and is executable
     





  • 相关阅读:
    值得学习的网站
    搜集的前端面试题,慢慢补充完答案
    vue2.0 transition 多个元素嵌套使用过渡
    vue2.0过度动画
    Vue中data、props、computed中数据的区别
    理解 $nextTick 的作用
    项目中使用better-scroll实现移动端滚动,报错:Cannot read property 'children' of undefined better-scroll
    less预编译语言使用总结
    vue项目配置less预编译语言
    CrossApp 0.3.8 发布,跨平台移动App开发引擎
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3239168.html
Copyright © 2011-2022 走看看