mardi 28 juin 2016

Multiple AND conditions in bash that work independently based on an OR condition separator?

This question is different from the multitude of potential duplicates, I've not been able to find this particular question answered or even asked...

Maybe I'm overlooking some simple logic here, but I'm running into the following issue:

I'm trying to have an if statement where conditions must be met like $V1 AND $V2 are TRUE || OR $V3 AND $V4 are TRUE. Here's a simple test:

#!/bin/bash

V1="File Placeholder"
#echo $V1
V2="May contain some text"
#echo $V2

V3="Some command output"
#echo $V3
V4="Command output contains this text"
#echo $V4

if [[ "$V1" ]] && [[ "$V2" == *"contain some"* ]] || [[ "$V3" ]] && [[ "$V4" == *"output contains"* ]]
then
echo "Hello $V1"
echo "World full of: $V2"
fi 

Meaning, I'd like to do something if:

$V1 is true (a file exists) AND $V2 is true (some string is found)

OR

$V3 is true (ie not null) AND $V4 is true (command output contains text)

It appears to work a bit, but I realize it's not working properly: it won't return TRUE if both the second && conditions are FALSE ie: || [[ "$V6" ]] && [[ "$V4" == *"outpuy contains"* ]] (why I think I may be overlooking some logic, maybe getting cancelled out some how?).

Why isn't this working as I assume it would if a AND b are TRUE ... OR ... if x AND z are TRUE?

Aucun commentaire:

Enregistrer un commentaire