MRKR
MRKR

Reputation: 1

If statement string comparison

What am I'm doing wrong over here? The script by default enters this IF statement & displays the echo statement to exit.

#!/bin/ksh


server=$1
dbname=$2
IFS="
"
if [[ "${dbname}" != "abc_def_data" || "${dbname}" != "abc_def01_data" ]]; then
        echo "Msg: Triggers can only be applied to CMS_JAD:abc_def_data/abc_def01_data!"
        exit 0
fi

Upvotes: 0

Views: 1164

Answers (3)

Sir. Hedgehog
Sir. Hedgehog

Reputation: 1300

try this man, it should work just fine you should have seperated the conditions with "[ ]" and used -o instead of ||.... btw it worked for me fine...

server=$1
dbname=$2
IFS=""

if [ "${dbname}" != "abc_def_data" ] -o [ "${dbname}" != "abc_def01_data" ]
        then
        echo "Msg: Triggers can only be applied to CMS_JAD:abc_def_data/abc_def01_data!"
        exit 0
fi

Upvotes: 0

shellter
shellter

Reputation: 37298

chaining of != conditions requires some inversion of thinking.

I much prefer a clearer path to testing these conditions by using the case ... esac structure.

case "${dbname}" in 
   abc_def_data|abc_def01_data ) 
        #dbg echo "matched, but for real code replace with just a ':' char" 
        :
   ;; 
   * ) 
       echo "didn_t match any expected values for \$dbname"
       echo exit 1
   ;; 
esac

Note that as you're really trying to find the *) case, the actions for the abc_def_data (etc) match can be anything, but to just skip to the next section of code, you would only need the shell's null cmd : .

Edit 1

Note that I have echo exit 1, just so if you copy/paste this to a command line, your shell won't exit. In real code, remove the echo and expect the exit to work.

Edit 2

Also, note that the | char in the case match (abc_def_data**|**abc_def01_data) is essentially an OR (I think it is called something else in the "case match" context).

IHTH

Upvotes: 1

Mithrandir
Mithrandir

Reputation: 25367

Did you, by any chance, meant to write this?

if [[ "${dbname}" != "abc_def_data" && "${dbname}" != "abc_def01_data" ]]; then
        echo "Msg: Triggers can only be applied to CMS_JAD:abc_def_data/abc_def01_data!"
        exit 0
fi

Upvotes: 0

Related Questions