繁体   English   中英

在鱼壳中,如何在 if 语句中放置两个条件?

[英]In fish shell, how can I put two conditions in an if statement?

我想用 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有两个条件?

另外两种方法:

方法一:

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

方法二:

false; or true
and echo y
or echo n

这种方式有效,但它是一个丑陋的黑客:

> 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

我真诚地希望得到更好的答案。

从fish 2.3b1 开始,可以直接在if条件中使用and / or链接命令。 begin ... end不再需要了。 官方文档于 2016 年 5 月更新

所以现在这按预期工作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM