如何在bash中做复杂的条件? (混合'和'&&,'或'|| ......)

时间:2012-04-23 22:03:38

标签: bash syntax conditional

如何在bash中完成以下内容?

if ("$a" == "something" || ($n == 2 && "$b" == "something_else")); then
  ...
fi

2 个答案:

答案 0 :(得分:22)

你几乎得到了它:

if [[ "$a" == "something" || ($n == 2 && "$b" == "something_else") ]]; then

实际上,由于运算符优先级,括号可以省略,所以它也可以写成

if [[ "$a" == "something" || $n == 2 && "$b" == "something_else" ]]; then

答案 1 :(得分:2)

if [[ "$a" == "something" ]] || [[ $n == 2 && "$b" == "something_else" ]]; then
  ...
fi