简体   繁体   English

bash,如何使用OR || If语句中的运算符?

[英]bash, how to use OR || operator in If statement?

Can anybody tell me what is wrong with this syntax: 谁能告诉我此语法有什么问题:

if [ "$1" == "postfix" || "$1" == "all" ]; then
    echo test
fi

Use either 使用任一

if [ "$1" = "postfix" ] || [ "$1" = "all" ]; then

or 要么

if [ "$1" = "postfix" -o "$1" = "all" ]; then

|| combines two commands (and the [ is a command). 合并两个命令( [是命令)。 Within the [ ... ] the operator -o is used for or . [ ... ] ,运算符-o用于

Another way to use || 使用||另一种方法 is to use it inside [[ instead of [ . 是在[[而不是[

In any way, the correct operator for equality is = inside [ ... ] as it is the POSIX standard (according to man bash ). 无论如何,相等的正确运算符是[ ... ]内部的=因为它是POSIX标准(根据man bash )。 == is supported, though. ==受支持。

If you want to do this, use [[ test form : 如果要执行此操作,请使用[[测试表格:

if [[ $1 == "postfix" || $1 == "all" ]; then

[[ is a bash keyword similar to (but more powerful than) the [ command. [[是bash关键字,与[命令类似,但功能更强大。 See http://mywiki.wooledge.org/BashFAQ/031 and http://mywiki.wooledge.org/BashGuide/TestsAndConditionals Unless you're writing for POSIX sh, we recommend [[ 请参阅http://mywiki.wooledge.org/BashFAQ/031http://mywiki.wooledge.org/BashGuide/TestsAndConditionals,除非您为POSIX sh编写,否则我们建议[[

or 要么

if [ "$1" == "postfix" ] || [ "$1" == "all" ]; then

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

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