Linux Shell Script - String Comparison with wildcards

前端 未结 1 1291
醉酒成梦
醉酒成梦 2020-12-29 23:01

I am trying to see if a string is part of another string in shell script (#!bin/sh).

The code i have now is:

#!/bin/sh
#Test scriptje to test string         


        
相关标签:
1条回答
  • 2020-12-29 23:33

    In bash you can write (note the asterisks are outside the quotes)

        if [[ $t1 == *"$t2"* ]]; then
                echo "$t1 and $t2 are equal"
        fi
    

    For /bin/sh, the = operator is only for equality not for pattern matching. You can use case though

    case "$t1" in
        *"$t2"*) echo t1 contains t2 ;;
        *) echo t1 does not contain t2 ;;
    esac
    

    If you're specifically targetting linux, I would assume the presence of /bin/bash.

    0 讨论(0)
提交回复
热议问题