Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在bashshell脚本中使用复合条件_Bash_Shell_Comparison - Fatal编程技术网

在bashshell脚本中使用复合条件

在bashshell脚本中使用复合条件,bash,shell,comparison,Bash,Shell,Comparison,我想在bash脚本中执行如下操作。如何用bash语法实现 if !((a==b) && (a==c)) then do something end if 你也可以使用我个人认为更清楚的以下内容: #!/bin/bash a=2 b=3 c=4 if (( (a != b) || (a != c) )); then # stuff here fi 从技术上讲,您不需要在子表达式周围使用paren,因为等式运算符===的优先级高于两个复合比较运算符和&&&|,但我认

我想在bash脚本中执行如下操作。如何用bash语法实现

if !((a==b) && (a==c))
then 
do something
end if
你也可以使用我个人认为更清楚的以下内容:

#!/bin/bash

a=2
b=3
c=4

if (( (a != b) || (a != c) )); then  
  # stuff here
fi

从技术上讲,您不需要在子表达式周围使用paren,因为等式运算符
===
的优先级高于两个复合比较运算符
和&&&|
,但我认为最好将它们保留在其中,以便在没有其他内容的情况下显示意图。

对于数值比较,您可以执行以下操作:

if [ "$a" != "$b" -o "$a" != "$c" ]; then
  # ...
else
  # ...
fi
if ! (( (a == b) && (a == c) ))
对于字符串比较:

if ! [[ "$a" == "$b" && "$a" == "$c" ]]

在Bash中,双括号设置了一个算术上下文(顺便说一句,其中美元符号大部分是可选的)用于比较(也用于((i=0;iOr,进行直接翻译:
if[!\(“$a”=“$b”\)-a\(“$a”=“$c”\)]
…像这样反转运算符的逻辑是。感谢Wilhelmtell。感谢简化逻辑检查感谢jonathan和chrisaycock也提供了很棒的提示:)谢谢SiegeX这里的精彩解释!谢谢Dennis。这正是我想要的解释双重偏执的+1语法类型!删除我的其他注释进行清理。
if ! [[ "$a" == "$b" && "$a" == "$c" ]]
if (true); then echo hi; fi 
if true; then echo hi; fi
if ($true)
if $true
if (($true))