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
     





  • 相关阅读:
    Nginx反向代理负载均衡
    Nginx服务rewrite模块功能说明 网站自动跳转功能
    利用C51单片机模拟SPI进行双机通信
    WIN10家庭版桌面右键单击显示设置出现ms-settings:display或ms-settings:personalization-background解决办法[原创]
    Docker for Mac k8s 网络不通
    Firefox 清理 DNS 缓存
    探索 IPv6 网络
    Firefox 印象笔记剪藏插件登录国内账号
    k8s ingress 转发服务,内容显示不全问题
    k8s 获取 Pod ip 添加到环境变量
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3239168.html
Copyright © 2011-2022 走看看