Sunday, February 27, 2022

[SOLVED] Complex if statement in bash

Issue

How to I write the following statement without getting Conditional binary operator expected Error?

if  [[ test "${EXPRESSION}"  || ( ( ! -f file1 )  && ( ! -f file2 ) && ( ! -f file3 ) ) ]]; then
doSomething

Solution

In bash

Testing that $EXPRESSION is not empty:

[[ $EXPRESSION ]]

Testing that there's no file file1, no file file2 and no file file3:

! [[ -f file1 ]] && ! [[ -f file2 ]] && ! [[ -f file3 ]]

whose logic could be expressed with: there's not any file file1, file2 or file3:

! [[ -f file1 || -f file2 || -f file3 ]]

So to sum it up:

if [[ $EXPRESSION ]] || ! [[ -f file1 || -f file2 || -f file3 ]]
then
    domSomething
fi


Answered By - Fravadona
Answer Checked By - Robin (WPSolving Admin)