美文网首页
linux shell 比较字符串是否相等

linux shell 比较字符串是否相等

作者: 风吟空城 | 来源:发表于2018-11-28 17:36 被阅读0次

    字符串比较

    linux shell中比较字符串是否相等,直接使用符号等号(=)即可,如:

    function check_param_two_correct(){
        local check_result=1;
        
        local node_name_list=`ls /data/tomcat | grep node`;
        for each in $node_name_list
        do
            if [ "$each" = "$1" ]
            then
                check_result=0;
                break;
            fi
        done
        
        return $check_result;   
    }
    

    注:

    • 等号两边需要有空格,shell要求;
    • 如果$each和$1同时为NULL,就会报错:[: =: unary operator expected,所以为了避免等号两边都为空,可以将上述比较改为:
    if [ "$each"a = "$1"a ]
    

    整数比较

    符号 说明
    -eq 等于,如:if [ "$a" -eq "$b" ]
    -ne 不等于,如:if [ "$a" -ne "$b" ]
    -gt 大于,如:if [ "$a" -gt "$b" ]
    -le 大于等于,如:if [ "$a" -ge "$b" ]
    -lt 小于,如:if [ "$a" -lt "$b" ]
    -le 小于等于,如:if [ "$a" -le "$b" ]
    > 大于(需要双括号),如:(("$a" > "$b"))
    >= 大于等于(需要双括号),如:(("$a" >= "$b"))

    相关文章

      网友评论

          本文标题:linux shell 比较字符串是否相等

          本文链接:https://www.haomeiwen.com/subject/kaszqqtx.html