logical condition
Hi
Using the following in bash doesn't produce the correct result. can any one spot why ? Code:
test= "A" |
Hi,
What output do you expect, 'cause it does produce the correct result (No Match). Logic: [ "$test" != "AB" ] || [ "$test" != "BC" ] is [ "A" != "AB" ] || [ "A" != "BC" ] is true || true is true and thus the then part is executed. Hope this helps. |
No - Since you don't tell us what YOUR expected result is and what result you are getting that you think is wrong.
However, a couple of things that I notice: 1) You have a space between test= and the variable - that is not correct - you need to remove the space. 2) You have quotes around the variable ("A") which are unnecessary and might be treated literally in some contexts. 3) You do not have a space before your final right bracket which means the test is not recognized as such. 4) You not need quotes around $test in your test statements. |
I am getting False result no match I have now tried to pass it in as command line
like so Code:
Why can i not get the exact match |
Hi,
That output is also correct: Logic: [ "$test" != "AB" ] || [ "$test" != "BC" ] [ "AB" != "AB" ] || [ "AB" != "BC" ] false || true true The then part (No match) is executed. Try not to use negative checks (!=) try positive once (==) whenever possible. Have a look at these 2 examples: Code:
#!/bin/bash |
Quote:
Basically, you're saying that it doesn't match if it's not "AB" or not "BC". If it's "AB", then it's not "BC", and you wrote that that means "No Match". Get it now? |
Moved to Programming
|
perl -e 'print ($test =~ /^(AB|BC)$/ ? "match" : "no match");'
so to speak ... it won't work quite as-writ but "you get the idea." A "shell command" can be written in any programming language, thanks to "shebang" (#!), and you've probably got half-a-dozen of 'em out there on your computer right now. |
Replace || with && and it will do what you want.
Russ |
All times are GMT -5. The time now is 08:51 AM. |