bluethundr
bluethundr

Reputation: 1325

enter an if statement if multiple conditions are true in bash

I am trying to enter an if/then statement only if these conditions are true:

if [ "$key1dtSec" -lt "$taSec" ] || [ "$key2dtSec" -lt "$taSec" ] && [[ ! -z "$user_lives_here" ]]; then

I want to enter the loop only if $key1dtSec OR $key2dtsec are true AND only if $user_lives_here has a value.

But the if/then statement gets executed even if $user_lives_here has a value. And I don't want that.

How can I ensure the if/then only happens if $user_lives_here has a value?

Upvotes: 2

Views: 45

Answers (2)

Ronan Boiteau
Ronan Boiteau

Reputation: 10138

You can group your conditions with parenthesis inside the [[ / ]] operators:

if [[ ! -z "$user_lives_here" && ( "$key1dtSec" -lt "$taSec" || "$key2dtSec" -lt "$taSec" ) ]]

Upvotes: 3

JNevill
JNevill

Reputation: 50064

You need to wrap your OR || condition in parentheses:

if  ( [ "$key1dtSec" -lt "$taSec" ] || [ "$key2dtSec" -lt "$taSec" ] ) && [[ ! -z "$user_lives_here" ]]; then

Upvotes: 2

Related Questions