comparing scalars contaning "DOUBLE QUOTES" as data

Hello to all,

Does anyone know the solution ?

Two strings A and B are present. I want to check whether B is a Substring of A.

  1. The value of A is - 29 * * * /bin/ls "test" "tmp*" "log*"
    (Note: Pl note that A contains DOUBLEQUOTES, ASTERISK & FRONTSLASH)

  2. The value of B is - /ls "test" "tmp*" "log*"
    (Note: again B contains DOUBLEQUOTES, ASTERISK & FRONTSLASH)

Since the variable scalars A and B have DOUBLEQUOTES as a part of the data stored "=~" operator is not working.

[ $A =~ $B ] - results in error..

Is there any other way (any sed/awk/perl one liners). Requesting help to solve this.

Thanks
rssrik

First of all, you need to learn to quote arguments properly. Simply double-quote anything with a variable in it, and you should be fine, most of the time.

If the strings also contain regex specials (in this case, asterisks), those need to be escaped somehow, or you need to use a different comparison function. May I suggest the plain old case statement?

case $A in *"$B"*) echo "$A contains $B";; *) echo does not;; esac

thanks .. it works

just wondering how come double quotes are escaped properly in case statement ! while [ "$A" =~ "$B" ] does not work ! seems like I have lot of catching up to do in shell basics ..

Thanks again era
rssrik

The double quotes are not the problem, it's that you are using the regular expression match operator on something which is just a string, and which happens to contain characters which have a special meaning in regular expressions. " " (space asterisk) matches zero or more spaces in a regular expression, but doesn't match a literal asterisk. "log" matches "lo", "log", "logg", "loggg", "logggg" etc but doesn't match "log*".

Gotcha !

Thanks to you "ERA"...

You can also use a conditional expression to test if B is a substring of A i.e.

$!/usr/bin/ksh93

# ANSI C string quoting!
A=$'29 * * * /bin/ls "test" "tmp*" "log*"'
B=$'/ls "test" "tmp*" "log*"'

[[ ${A} == *${B}* ]] && print "Yes, B is a substring of A"