如果bash中有多个条件为true,则输入if语句

如果bash中有多个条件为true,则输入if语句,bash,shell,Bash,Shell,仅当以下条件为真时,我才尝试输入if/then语句: if [ "$key1dtSec" -lt "$taSec" ] || [ "$key2dtSec" -lt "$taSec" ] && [[ ! -z "$user_lives_here" ]]; then 仅当$key1dtSec或$key2dtsec为真且仅当$user\u lived\u此处有值时,我才想进入循环 但是如果/则执行语句,即使$user\u lived\u这里有值。我不想那样 如何确保如果/那么只有在$

仅当以下条件为真时,我才尝试输入if/then语句:

if [ "$key1dtSec" -lt "$taSec" ] || [ "$key2dtSec" -lt "$taSec" ] && [[ ! -z "$user_lives_here" ]]; then
仅当
$key1dtSec
$key2dtsec
为真且仅当
$user\u lived\u此处
有值时,我才想进入循环

但是
如果
/
则执行
语句,即使
$user\u lived\u这里
有值。我不想那样


如何确保
如果
/
那么
只有在
$user\u lived\u这里
有值时才会发生?

您需要将您的或
|
条件括在括号中:

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

您可以在
[[[
/
]]
运算符中使用括号将条件分组:

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

使用
{…}
将更有效,以避免不必要的子shell。答案是好的,但不必要的混乱。试试:
如果[$user_住在这里&&(key1dtSec-lt-taSec | | key2dtSec-lt-taSec)]