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

    算术运算的条件判断
    [] [[]]:
    -eq
    -ne
    -lt
    -le
    -gt
    -ge

    (( )):
    >
    <
    >=
    <=
    =

    [root@monitor ~]# if (( 2 == 3));then echo '123'; fi
    [root@monitor ~]# if (( 2 >= 3));then echo '123'; fi
    [root@monitor ~]# if (( 2 <= 3));then echo '123'; fi
    123
    [root@monitor ~]# if (( 2 < 3));then echo '123'; fi
    123
    [root@monitor ~]# if (( 2 > 3));then echo '123'; fi

    
    
    字符串的条件判断
    
    -z
    -n
    =
    ==
    !=
    <
    >
    
    文件属性的条件判断
    
    -f
    -d
    -c
    -w
    -L
    -x
    -e
    -b
    -r



    #!/bin/bash
    if [ -e demo.sh ];then
    echo "文件存在"
    fi

    fpath="/etc/passwd"

    if [ -e $fpath ];then
    echo file exists;
    else
    echo file no exists;
    fi


    [ -e "/etc/hosts" ] || (echo '/etc/hosts not exist";exit 1)

    if [ "$?" -eq 1 ];then
    exit
    fi

    echo "/etc/hosts 文件存在"


    declare -i a
    a=20
    if [ $a -eq 20 ];then
    echo "var a 20"
    fi


    if [ $a -gt 10 ];then
    echo 'var >10';
    fi


    if [ "$LOGNAME" != "ROOT" ];then
    echo "root "
    fi


    if [ "Bill" >"Apple" ];then
    echo " BILL >APPLE"
    fi

    str="Bill"
    if [ -n $str];then
    echo "234"
    fi

    #!/bin/bash
    NUM1=100
    NUM2=200
    if (($NUM1 > $NUM2));then
    echo "ok"
    else
    echo "ok1"
    fi

    #!/bin/bash
    Dir=/tmp/20140909
    if [ ! -d $Dir ];then
    mkdir -p $Dir
    echo -e "33[32mthis $Dir exist33[0m"
    else
    echo -e "33[32mthis $Dir is exist,please exit.33[0m"

    fi


    #!/bin/bash
    FILES=/tmp/test.txt
    if [ -f $FILES ];then
    echo "ok">>$FILES
    else
    cat $FILES
    fi

    -a: 逻辑表达式 -a 逻辑表达式
    -o: 逻辑表达式 -o 逻辑表达式

  • 相关阅读:
    10、Python的while与死循环
    8、 Python的if分支练习题
    7、 Python中的if多重判断
    6、Python的if判断和两重判断
    5、运算符
    4、数据类型:字典
    placeholder 颜色更改
    禁止video在苹果手机上的自动全屏播放
    点击label出发两次点击事件
    instanceof 和 typeof
  • 原文地址:https://www.cnblogs.com/zengkefu/p/5522976.html
Copyright © 2011-2022 走看看