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
 





原文地址:https://www.cnblogs.com/javawebsoa/p/3239168.html