• shell 判断字符串包含的5种方法


    strA="long string"
    strB="string"
    result=$(echo $strA | grep "${strB}")
    if [[ "$result" != "" ]]
    then
        echo "包含"
    else
        echo "不包含"
    fi
    
    
    
    strA="helloworld"
    strB="low"
    if [[ $strA =~ $strB ]]
    then
        echo "包含"
    else
        echo "不包含"
    fi
    
    
    
    
    A="helloworld"
    B="low"
    if [[ $A == *$B* ]]
    then
        echo "包含"
    else
        echo "不包含"
    fi
    
    
    
    
    
    thisString="1 2 3 4 5" # 源字符串
    searchString="1 2" # 搜索字符串
    case $thisString in
        *"$searchString"*) echo "包含" ;;
        *) echo "不包含" ;;
    esac
    
    
    
    
    STRING_A=$1
    STRING_B=$2
    if [[ ${STRING_A/${STRING_B}//} == $STRING_A ]];then
        ## is not substring.
        echo "包含"
        exit 0
    else
        ## is substring.
        echo "不包含"
        exit 1
    fi

    参考:

    网站 stackoverflow 以及segmentfault

  • 相关阅读:
    053-49
    053-3
    053-204
    053-491
    053-205
    053-57
    053-149
    053-47
    053-150
    回答2
  • 原文地址:https://www.cnblogs.com/sea-stream/p/11414105.html
Copyright © 2020-2023  润新知