Reputation: 99
I have the problem that I want an if statement that makes sure the first statement is true and one or more of the following three statement are also true in a bash script.
The code I currently have is:
if [[ $TAIL != "true" ]] && [[ -f $FILE_DIR/.completed ]] || [[ -f $FILE_DIR/../.completed ]] || [[ -f $FILE_DIR/../../.completed ]]
I have also tried:
if [[ $TAIL != "true" ]] && [[ [[ -f $FILE_DIR/.completed ]] || [[ -f $FILE_DIR/../.completed ]] || [[ -f $FILE_DIR/../../.completed ]] ]]
but neither work.
Anyone any ideas on how to fix this?
Thanks in advance for help.
The answer is as shown below:
if [[ $TAIL != "true" ]] && ( [[ -f $FILE_DIR/.completed ]] || [[ -f $FILE_DIR/../.completed ]] || [[ -f $FILE_DIR/../../.completed ]] )
Upvotes: 2
Views: 2832
Reputation: 4935
You can do it all inside one [[]]:
if [[ $TAIL != "true" && ( -f $FILE_DIR/.completed || -f $FILE_DIR/../.completed || -f $FILE_DIR/../../.completed ) ]]
Upvotes: 3
Reputation: 4117
you can group your OR statements with parentheses:
if [[ $TAIL != "true" ]] && ( [[ -f $FILE_DIR/.completed ]] || [[ -f $FILE_DIR/../.completed ]] || [[ -f $FILE_DIR/../../.completed ]] )
Upvotes: 2