Bash 中的 if Not 條件

  1. Bash 中整數情況下的 if not 條件
  2. Bash 中字串的 if not 條件
  3. 表示式中的 Bash if not 條件
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
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe

相關文章 - Bash Condition