If statement 在fish shell中,如何在if语句中放入两个条件?

If statement 在fish shell中,如何在if语句中放入两个条件?,if-statement,fish,If Statement,Fish,我想用bash做什么: > if true; then echo y; else echo n; fi y > if false; then echo y; else echo n; fi n > if false || true; then echo y; else echo n; fi y 现在尝试鱼: > if true; echo y; else; echo n; end y > if false; echo y; else; echo n; end n

我想用bash做什么:

> if true; then echo y; else echo n; fi
y
> if false; then echo y; else echo n; fi
n
> if false || true; then echo y; else echo n; fi
y
现在尝试鱼:

> if true; echo y; else; echo n; end
y
> if false; echo y; else; echo n; end
n

# Here 'or true' are just two arguments for false
> if false or true; echo y; else; echo n; end 
n

# Here 'or true;' is a command inside the if
> if false; or true; echo y; else; echo n; end 
n

# Can't use command substitution instead of a command
> if (false; or true); echo y; else; echo n; end
fish: Illegal command name “(false; or true)”

如果,我怎么能在一个
中有两个条件呢?

这种方法可行,但它是一个丑陋的黑客行为:

> if test (true; and echo y); echo y; else; echo n; end 
y
> if test (false; and echo y); echo y; else; echo n; end 
n
> if test (false; or true; and echo y); echo y; else; echo n; end 
y
我真诚地希望得到更好的答案。

还有两种方法:

方法一:

if begin false; or true; end
  echo y
else
  echo n
end
方法二:

false; or true
and echo y
or echo n
,可以直接将
if
条件下的命令与
/
链接。
开始。。。不再需要结束
。官方文件

因此,现在这项工作正如预期的那样:

> if false; or true; echo y; else; echo n; end
y

第一种方法可能就是我想要的,但是第二种方法很酷,但是太模糊了。我同意。我搞不清楚为什么/如何使用第二种方法。有人愿意解释吗?这很简单:如果条件1或条件2为真,它将继续,这意味着它将输入and语句并一起计算。由于and的结果是真实的,因此不需要运行最后一个or语句。但是,如果条件1和条件2都是负数,并且语句提前取消,则无需计算第二部分。该语句的结果将变为false,因此最后一个or语句将运行。对于上的简单条件,不需要
begin。。。结束
;这里,
如果为false;或true
可以正常工作。@clozach它只是在最后一个命令状态值状态下工作,您可以在语法中选择一个较大的块格式,可能用于多个命令或可读性,或者选择一个较小的格式,只需要一条语句,但您也可以使用
begin;…;手动扩展到更大的块中。。。;结束
。在这一点上,你只是选择了传统。