zoukankan      html  css  js  c++  java
  • 条件判断

    基本语法

    [ condition ] (注意 condition 前后要有空格)

    注意:条件非空即为true,[ atguigu ]返回true,[] 返回false

    常用判断条件

    两个整数之间的比较

    运算符说明
    == 相等,用于比较两个数,相同则返回true
    != 不相等,用于比较两个数,不相同则返回true
    = 字符串比较
    -lt 小于(less than)
    -le 小于等于(less equal)
    -eq 等于(equal)
    -gt 大于(greater than)
    -ge 大于等于(greater equal)
    -ne 不等于(not equal)

    按照文件权限进行判断

    运算符说明
    -r 有读的权限(read)
    -w 有写的权限(write)
    -x 有执行的权限(execute)

    按照文件类型进行判断

    运算符说明
    -f 文件存在并且是一个常规文件(file)
    -e 文件存在(existence)
    -d 文件存在并是一个目录

    案例实操

    [root@slave2 testshell]# [ 22 -le 23 ]
    [root@slave2 testshell]# echo $?      
    0
    [root@slave2 testshell]# [ 22 -ge 23 ] 
    [root@slave2 testshell]# echo $?      
    1
    [root@slave2 testshell]# ll
    total 16
    -rw-r--r--. 1 root root  85 Jan 18 08:18 addwords.sh
    -rwxrwxrwx. 1 root root 318 Jan 18 21:24 ArithmeticOperation.sh
    -rw-r--r--. 1 root root  11 Jan 18 08:18 banzhang.txt
    -rwxrwxrwx. 1 root root  54 Jan 18 10:05 parameter.sh
    [root@slave2 testshell]# [ -w addwords.sh ]
    [root@slave2 testshell]# echo $?
    0
    [root@slave2 testshell]# [ -r addwords.sh ] 
    [root@slave2 testshell]# echo $?           
    0
    [root@slave2 testshell]# [ -e addwords.sh ] 
    [root@slave2 testshell]# echo $?           
    0
    [root@slave2 testshell]# [ -f addwords.sh ] 
    [root@slave2 testshell]# echo $?           
    0
    [root@slave2 testshell]# [ -d addwords.sh ] 
    [root@slave2 testshell]# echo $?           
    1
    [root@slave2 testshell]# [ -x addwords.sh ] 
    [root@slave2 testshell]# echo $?           
    1

     多条件判断

    &&:表示前一条命令执行成功时,才执行后一条命令

    ||:表示上一条命令执行失败后,才执行下一条命令

    [root@slave2 testshell]# [ -x addwords.sh ] && echo ok || echo 'not ok'
    not ok
    [root@slave2 testshell]# [ -r addwords.sh ] && echo ok || echo 'not ok' 
    ok
  • 相关阅读:
    范仁义css3课程---5、css的继承、层叠和特殊性
    范仁义css3课程---4、css常用选择器
    心得体悟帖---200103(路是我自己选的)
    心得体悟帖---200103(变化的观点)(我是对的)
    心得体悟帖---200103(开心与否更看内心)(不要丢失希望)
    心得体悟帖---200103(看似感伤)(不计较)
    windows的80端口被system进程占用的一个可能原因
    windows如何关闭mysql服务
    范仁义css3课程---3、css最常用选择器
    ImageView类简介
  • 原文地址:https://www.cnblogs.com/zxbdboke/p/10416072.html
Copyright © 2011-2022 走看看