• linux下字符串的比较方式


    A="$1"
    B="$2"
      
    #判断字符串是否相等
    if [ "$A" = "$B" ];then
    echo "[ = ]"
    fi
      
    #判断字符串是否相等,与上面的=等价
    if [ "$A" == "$B" ];then
    echo "[ == ]"
    fi
      
    #注意:==的功能在[[]]和[]中的行为是不同的,如下
      
    #如果$a以”a”开头(模式匹配)那么将为true 
    if [[ "$A" == a* ]];then
    echo "[[ ==a* ]]"
    fi
      
    #如果$a等于a*(字符匹配),那么结果为true
    if [[ "$A" == "a*" ]];then
    echo "==/"a*/""
    fi
      
      
    #File globbing(通配) 和word splitting将会发生, 此时的a*会自动匹配到对应的当前以a开头的文件
    #如在当前的目录中有个文件:add_crontab.sh,则下面会输出ok
    #if [ "add_crontab.sh" == a* ];then 
    #echo "ok"
    #fi
    if [ "$A" == a* ];then
    echo "[ ==a* ]"
    fi
      
    #如果$a等于a*(字符匹配),那么结果为true
    if [ "$A" == "a*" ];then
    echo "==/"a*/""
    fi
      
    #字符串不相等
    if [ "$A" != "$B" ];then
    echo "[ != ]"
    fi
      
    #字符串不相等
    if [[ "$A" != "$B" ]];then
    echo "[[ != ]]"
    fi
      
    #字符串不为空,长度不为0
    if [ -n "$A" ];then
    echo "[ -n ]"
    fi
      
    #字符串为空.就是长度为0.
    if [ -z "$A" ];then
    echo "[ -z ]"
    fi
      
    #需要转义<,否则认为是一个重定向符号
    if [ $A /< $B ];then
    echo "[ < ]"  
    fi
      
    if [[ $A < $B ]];then
    echo "[[ < ]]"  
    fi
      
    #需要转义>,否则认为是一个重定向符号
    if [ $A /> $B ];then
    echo "[ > ]"  
    fi
      
    if [[ $A > $B ]];then
    echo "[[ > ]]"  
    fi
  • 相关阅读:
    PHP面试题遇到的几个坑。...面壁ing
    Java基础- super 和 this 解析
    openStack use
    ceph伦理概念
    openstack core components use 总结
    current imporant Posts
    openNebula rgister img instance vms error collections
    openStack images概念及维护
    Error copying image in the datastore: Not allowed to copy image file
    OpenNebula openldap集成
  • 原文地址:https://www.cnblogs.com/xingyunfashi/p/7643672.html
Copyright © 2020-2023  润新知