在 Bash 中使用多个表达式复合 if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11267569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:38:34  来源:igfitidea点击:

Compound if statements with multiple expressions in Bash

bashif-statementsyntax

提问by david

I would like to recreate something like this

我想重新创建这样的东西

if ( arg1 || arg2 || arg 3) {}

and I did got so far, but I get the following error

到目前为止,我确实做到了,但出现以下错误

line 11: [.: command not found

if [ $char == $';' -o $char == $'\' -o $char == $'\'' ]
then ...

I tried different ways but none seem to work some of the ones I tried

我尝试了不同的方法,但似乎没有一种方法对我尝试过的一些方法有效

回答by Charles Duffy

For Bash, you can use the [[ ]]form rather than [ ], which allows &&and ||internally:

对于 Bash,您可以使用[[ ]]form 而不是[ ],它允许&&和在||内部:

if [[ foo || bar || baz ]] ; then
  ...
fi

Otherwise, you can use the usual Boolean logic operators externally:

否则,您可以在外部使用常用的布尔逻辑运算符:

[ foo ] || [ bar ] || [ baz ]

...or use operators specific to the testcommand (though modern versions of the POSIX specification describe this XSI extension as deprecated -- see the APPLICATION USAGE section):

...或使用特定于test命令的运算符(尽管现代版本的 POSIX 规范将此 XSI 扩展描述为已弃用——请参阅应用程序用法部分):

[ foo -o bar -o baz ]

...which is a differently written form of the following, which is similarly deprecated:

...这是以下内容的不同书写形式,同样被弃用:

test foo -o bar -o baz

回答by kojiro

Charles' answeris correct in that it shows you how to do logical operations on commands within (and without, for that matter) an ifstatement, but it looks more like you want to use casehere:

Charles 的回答是正确的,因为它向您展示了如何对if语句内(和不在内)的命令进行逻辑操作,但它看起来更像是您想在case这里使用:

case $char in
    \;|\|\') echo found;;
    *) echo 'not found';;
esac

回答by codeforester

Bash's [[ ]]and (( ))are more powerful and flexible than [ ].

Bash 的[[ ]](( ))[ ].

  • [[ ]]is for strings and (( ))is for integer logic and arithmetic
  • &&and ||operators can be used inside [[ ]]and (( )), and ()can be used for grouping
  • No need to quote variable expansions inside [[ ]]or (( ))- Bash doesn't do word splittingor globbing in these contexts
  • Inside (( )), there is no need for a $behind variable names to expand them
  • [[ ]]and (( ))can span multiple lines, without the need for a line continuation with \
  • [[ ]]用于字符串,(( ))用于整数逻辑和算术
  • &&and||运算符可以在[[ ]]and内部使用(( ))()也可以用于分组
  • 无需在内部引用变量扩展[[ ]](( ))- Bash在这些上下文中不进行分或通配
  • 在里面(( )),不需要$后面的变量名来扩展它们
  • [[ ]]并且(( ))可以跨越多行,而无需行延续\

Using these, we can write clean, readable, and more reliable code.

使用这些,我们可以编写干净、可读且更可靠的代码。

Examples

例子

Compound statements with integers

带整数的复合语句

a=1 b=2 c=3
((a == 2 || (b == 2 && c == 3))) && echo yes                   # yields yes

Compound statements with strings

带字符串的复合语句

x=apple y=mango z=pear
[[ $x == orange || ($y == mango && $z == pear) ]] && echo yes  # yields yes

[ equivalents for the above statements, using {}

[ 上述语句的等价物,使用 {}

[ "$a" -eq 2 ] || { [ "$b" -eq 2 ] && [ "$c" -eq 3 ]; }
[ "$x" == orange ] || { [ $y == mango ] && [ "$z" == pear ]; }


Related

有关的