0

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 
5
  • you never use $server, is that intentional? Commented Mar 29, 2016 at 14:22
  • 1
    The condition is always true. Commented Mar 29, 2016 at 14:26
  • i only copied a part of the code... $server will be used later in the script... @Daniele Commented Mar 29, 2016 at 14:28
  • I felt so... But my intentions are to check the parameter & exit if the name doesn't match any of the 2 names i mentioned... @Mithrandir Commented Mar 29, 2016 at 14:30
  • I guessed as much. :-) Commented Mar 29, 2016 at 14:35

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

for every "dbname", the script gets into the IF statement & exits. my intentions are, when the dbname is not equal to abc_def_data or abc_def01_data, the script has to print that echo statement & exit... if dbname is equal to any of them both, it should not EXIT & go to the next step.
yes, I read and understood your question at the top. Did you try replacing your if ... fi block with this case ... esac block? It should work as you describe. Note that you can extend the list of OK as "wide" as you want like abc_def_data|abc_def01_data|bcd_def_data|bcd_def01_data|.... . Good luck.
0

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 

5 Comments

i'm confused here... i want the script to EXIT if the dbname is not equal to either abc_def_data or abc_def01_data.
Because that what my need is. The script has a task to do & it needs to be applied to a particular set of "dbname"s.
The Script will now exit, if the value of ${dbname} is neither "abc_def_data", nor "abc_def01_data". What is the confusing part?
@MRKR : Mentally, we construct these problems as lists of ORs. but for Boolean logic, it has to be expressed as &&s . Turn on shell debugging/trace with set -vx and look at what happens on expanded variables on the lines preceded with +. To further understand, replace one of the tests with true and then change that to false and think about the rules of logic and how it all connects. The shell is working correctly ;-) Good luck.
for every "dbname", the script gets into the IF statement & exits. my intentions are, when the dbname is not equal to abc_def_data or abc_def01_data, the script has to print that echo statement & exit... if dbname is equal to any of them both, it should not EXIT & go to the next step.
0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.