Trying to check for 3 conditions in one line of code, but I'm stuck.
Essentially, I need to code for the following:
IF
string1 is not equal to string2
AND
string3 is not equal to string4
OR
bool1 = true
THEN
display "conditions met - running code ...".
As requested in the comments, I've updated my example to try to make the problem clearer.
#!/bin/sh
string1="a"
string2="b"
string3="c"
string4="d"
bool1=true
the easy-to-read way ....
if [ "$string1" != "$string2" ] && [ "$string3" != "$string4" ] ; then
echo "conditions met - running code ..."
fi
if $bool1 ; then
echo "conditions met - running code ..."
fi
or the shorter way ...
[ "$string1" != "$string2" ] && [ "$string3" != "$string4" ] && echo "conditions met - running code ..."
$bool1 && echo "conditions met - running code ..."
The code above will potentially run twice: if the first 2 conditions are met, and then again if the 3rd condition is met. This is not what I need.
The issue with this example is that it involves 2 distinct calls to 'echo' - (note: in the real code, it's not an echo, but you get the idea). I'm trying to reduce the code duplication by combining the 3 condition check into a single command.
I'm sure there's a few people now shaking their heads and shouting at the screen "That's NOT how you do it!"
And there's probably others waiting to mark this as a duplicate ... well, I looked but I'm damned if I could figure out how to do this from the answers I've read.
Can someone please enlighten me ? :)
A && (B || C)
or(A && B) || C
? see http://stackoverflow.com/a/6270803/137158 – cas May 05 '16 at 07:55if (A AND B) OR C
? I'm still not 100% sure that's what you need. Also, what system will this be running on? Do you really mean bourne shell (sh
), or is that bash called assh
(which is the default on many systems) ordash
calledsh
(which is the default on Ubuntu and perhaps others). – terdon May 05 '16 at 09:28[[ "$string1" != "$string2" ] && "$string3" != $string4" ]] || "$bool1" && echo "continue"
. – terdon May 05 '16 at 09:54