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 stackoverflow.com/a/6270803/137158