为什么在bash中使用短路布尔逻辑而不是if/then/else? [英] Why use short-circuiting boolean logic rather than if/then/else in bash?

查看:139
本文介绍了为什么在bash中使用短路布尔逻辑而不是if/then/else?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在上周我想做的事情上,我现在要进行更多的bash编程.

So I'm now doing quite a bit more bash programming in the last week that I thought I'd do.

我看过使用[ condition ] && { ... }if [ condition ]; then ...; fi类型构造的代码.

I've seen code that uses [ condition ] && { ... } and if [ condition ]; then ...; fi type constructs.

据我了解,[ condition ] && { ... }等同于if [ condition ]; then ...; fi.

但是,使用前一种构造,您将无法进行其他构造.

However with the former construct you can't do an else.

有人可以解释一下您为什么要在另一个上使用它的优缺点,以及什么是最佳实践.

Can someone please explain why you'd use one over the other, the advantages/disadvantages and what would be best practice here.

推荐答案

[[ ]]是ksh中的内置条件语法,被bash,zsh等使用.可以将它视为功能更强大的[命令版本(这是该命令的另一个名称,也称为test;要清楚,[命令,不是句法).它不是一个流控制运算符,因此在有关流控制的讨论中基本上没有主题.

[[ ]] is built-in conditional syntax in ksh, adopted by bash, zsh and others. Think of it as a more powerful version of the [ command (which is a different name for the command also called test -- and to be clear, [ is a command, not syntax). It is not a flow control operator, and thus largely off-topic in a discussion about flow control.

some-command && other-command是短路布尔逻辑.也就是说,这是一条返回两个命令是否都返回真实值的语句.

some-command && other-command is short-circuiting boolean logic. That is to say, it's a statement that returns whether both commands return truthful values.

当且仅当some-command成功时,才有必要运行other-command来了解两个命令是否都成功.因此,作为此逻辑运算符的副作用,它的行为类似于if语句.

If and only if some-command succeeds, it is necessary to run other-command to know whether both commands succeed. Thus, as a side effect of this logic operator, it happens to behave in a manner similar to an if statement.

同样,some-command || other-command返回一个值,该值指示命令之一或另一个是否成功.为了确定两者之一是否正确,仅当左边的那个失败时,才需要在右边运行一个.如果左边的一个成功,则先验地知道两者之一已经成功.

Similarly, some-command || other-command returns a value which indicates whether one of the commands or the other has succeeded. In order to determine if either of the two is true, it is only necessary to run the one on the right if the the one on the left fails; if the one on the left succeeds, then it is already known a priori that one of the two has succeeded.

此行为以及相关的惯用法对于所有实现具有明确定义的操作顺序的短路布尔逻辑的语言都是通用的.没有任何一部分专门针对bash.

那么,为什么要使用短路形式?简洁

So, why would you use the short-circuiting form? Terseness.

为什么要使用长格式?正确性,特别是在需要组合操作的情况下.考虑:

Why would you use the long form? Correctness, particularly in cases where one needs to combine operations. Consider:

foo && bar || baz

这很像:

if foo; then bar; else baz; fi

...但是,它有一个错误:如果bar失败,则即使foo成功,也可以运行baz.因此,除非人们对布尔逻辑情况下的期望行为非常确定,否则,if/then/else形式将是首选.

...however, it has a bug: If bar fails, then baz can be run even if foo succeeded. Thus, the if/then/else form is to be preferred unless one is very certain about desired behavior in the boolean-logic case.

这篇关于为什么在bash中使用短路布尔逻辑而不是if/then/else?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆