Bash 中的 if Not 条件

在 bash 中,如果命令列表的代码为真,那么 if | then 语句在单词 then 之后执行一个或多个命令。如果检查返回 false,则在条件需要时执行 else。我们将在这里学习制作如果不是条件。

Bash 中整数情况下的 if not 条件

我们可以使用 -ne 来检查两个整数变量之间的不等式。让我们看一个例子。

#!/bin/bash
x=5
y=4
if [[ $x -ne $y ]];
then
  echo "Not equal"
fi

输出:

Not equal

Bash 中字符串的 if not 条件

我们可以使用 != 运算符来比较两个字符串。

#!/bin/bash
sone=Bin
stwo=Bash
if [[ $sone != $stwo ]];
then
  echo "Not equal"
fi

输出:

Not equal

表达式中的 Bash if not 条件

我们可以使用! [[]] 之外的运算符使整个表达式的输出为负数。我们不能在双方括号内进行操作以使单个表达式为负数。让我们看一个例子。

#!/bin/bash
n=4
if ! [[ $n -eq 0 ]];
then
  echo "Not equal to 0"
fi

输出:

Not equal to 0