cannot properly employ "or" operator in an if statement (bash)

Hi,

I have a variable, $sername, and I would like to display this variable only if it does not contain either of these two tags: *DTI*FA* or *DIFF*FA*.

I think the syntax for my 'or' operator is off. The variable $sername is continuously changing in an outer loop (not shown), but at the moment, as an example, let's say:

sername=field_mapping

if [[ $sername == *DIFF*FA* || *DTI*FA* ]]; then echo "Found an FA tag in sername"; else  echo "The variable sername is FA-free"; fi

But this statement keeps outputting, "Found an FA tag in sername", which isn't right. Can anyone help me with the syntax? It would be nice if it could be done in a case-insensitive manner, too.

Thanks in advance!

What shell are you using? Checking if a string contains a string isn't a single-operator task I think.

[edit] ah, bash.

function contains # haystack needle
{
        local H="$1"
        local N="$2"
        local C="${#H}"
        H="${H/$N/}"

        [ "${#H}" -lt "${C}" ] && return 0
        return 1
}

contains ASDF G || contains ASDF H || echo "Doesn't contain these things"

contains ASDF A || contains ASDF B || echo "Doesn't contain these things"

to use 'or' in if in bash or bourne , do this:

if [ blah ] || [ blah ] || [ blah ]; then
blah blah blah

fi

If I try sheelscripter's suggestion:


$sername=field_mapping

if [ $sername == *map* ] || [ $sername == *DTI*FA* ]; then echo "Found an FA tag in sername"; else  echo "The variable sername is FA-free"; fi

it is not specific enough, i.e., even though there's the tag "map" in the variable $sername, and I am now calling that in the 'if' statement, it still says that that variable does not contain the tag. In other words, the echo command delivers: "The variable sername is FA-free". So it didn't work.

Any other suggestions? This is bash, by the way. Thanks!

Your original if statement was nearly correct. There must be a complete expression on either side of the or:

if [[ $sername == *DIFF*FA* || $sername ==  *DTI*FA* ]]

Your second attempt isn't working because

[ expression ]

is very much different than

[[ expression ]]

The double bracket expression is interpreted by the shell and the right hand side is treated as a pattern (if not quoted). Single brackets are treated just like any any other command (remember that [ is a link to the test command). If wildcards (* and ?) are used inside of single brackets, they are expanded as file names in the current directory and NOT treated as patterns. The filename expansion is then passed to the test command.

In my opinion it is best to avoid using single brackets in if and while statements in Kshell and bash. Forking to the test command is very inefficient not to mention the enhancements to the expressions that Kshell and bash provide.