

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 AWS TOE 元件文件中使用比較運算子
<a name="toe-comparison-operators"></a>

您可以搭配 **[聲明](toe-action-modules.md#action-modules-assertion)**動作模組和使用 的條件式表達式使用下列比較運算子[如果建構語法](toe-conditional-constructs.md#toe-conditional-if)。比較運算子可以在單一值上操作，例如 `stringIsEmpty`，也可以比較基準值與第二個值 （變數值），以判斷條件式表達式是否評估為 `true`或 `false`。

如果比較操作於兩個值，則第二個值可以是鏈結變數。

比較不同類型的值時，下列值轉換可能會在比較之前發生：
+ 對於數值比較，如果變數值是字串， 會在評估之前將字串 AWS TOE 轉換為數字。如果無法轉換，比較會傳回 `false`。例如，如果變數值為 `"1.0"`，則轉換會運作，但如果變數值為轉換`"a10"`會失敗。
+ 對於字串比較，如果變數值是數字， 會在評估之前將其 AWS TOE 轉換為字串。

## 比較字串
<a name="toe-compare-strings"></a>

下列比較運算子使用字串來比較值、測試空格或空字串，或比較輸入值與規則運算式模式。字串比較不區分大小寫，而且不會從字串輸入的開頭或結尾修剪空格。

**字串比較運算子**
+ [stringIsEmpty](#stringIsEmpty)
+ [stringIsWhitespace](#stringIsWhitespace)
+ [stringEquals](#stringEquals)
+ [stringLessThan](#stringLessThan)
+ [stringLessThanEquals](#stringLessThanEquals)
+ [stringGreaterThan](#stringGreaterThan)
+ [stringGreaterThanEquals](#stringGreaterThanEquals)
+ [patternMatches](#patternMatches)

**stringIsEmpty**  
`true` 如果指定的字串不包含任何字元，運算`stringIsEmpty`子會傳回 。例如：  

```
# Evaluates to true
stringIsEmpty: ""

# Evaluates to false
stringIsEmpty: " "
				
# Evaluates to false
stringIsEmpty: "Hello."
```

**stringIsWhitespace**  
測試 指定的字串是否只`stringIsWhitespace`包含空格。例如：  

```
# Evaluates to true
stringIsWhitespace: "   "

# Evaluates to false
stringIsWhitespace: ""
				
# Evaluates to false
stringIsWhitespace: " Hello?"
```

**stringEquals**  
測試 指定的字串`stringEquals`是否與 `value` 參數中指定的字串完全相符。例如：  

```
# Evaluates to true
stringEquals: 'Testing, testing...'
value: 'Testing, testing...'

# Evaluates to false
stringEquals: 'Testing, testing...'
value: 'Hello again.'

# Evaluates to false
stringEquals: 'Testing, testing...'
value: 'TESTING, TESTING....'

# Evaluates to false
stringEquals: 'Testing, testing...'
value: '   Testing, testing...'
				
# Evaluates to false
stringEquals: 'Testing, testing...'
value: 'Testing, testing...   '
```

**stringLessThan**  
測試 指定的字串是否`stringLessThan`小於 `value` 參數中指定的字串。例如：  

```
# Evaluates to true
# This comparison operator isn't case sensitive
stringlessThan: 'A'
value: 'a'

# Evaluates to true - 'a' is less than 'b'
stringlessThan: 'b'
value: 'a'

# Evaluates to true
# Numeric strings compare as less than alphabetic strings
stringlessThan: 'a'
value: '0'

# Evaluates to false
stringlessThan: '0'
value: 'a'
```

**stringLessThanEquals**  
測試為 指定的字串是否`stringLessThanEquals`小於或等於 `value` 參數中指定的字串。例如：  

```
# Evaluates to true - 'a' is equal to 'a'
stringLessThanEquals: 'a'
value: 'a'

# Evaluates to true - since the comparison isn't case sensitive, 'a' is equal to 'A'
stringLessThanEquals: 'A'
value: 'a'

# Evaluates to true - 'a' is less than 'b'
stringLessThanEquals: 'b'
value: 'a'

# Evaluates to true - '0' is less than 'a'
stringLessThanEquals: 'a'
value: '0'

# Evaluates to false - 'a' is greater than '0'
stringLessThanEquals: '0'
value: 'a'
```

**stringGreaterThan**  
測試 指定的字串是否`stringGreaterThan`大於 `value` 參數中指定的字串。例如：  

```
# Evaluates to false - since the comparison isn't case sensitive, 'A' is equal to 'a'
stringGreaterThan: 'a'
value: 'A'

# Evaluates to true - 'b' is greater than 'a'
stringGreaterThan: 'a'
value: 'b'

# Evaluates to true - 'a' is greater than '0'
stringGreaterThan: '0'
value: 'a'

# Evaluates to false - '0' is less than 'a'
stringGreaterThan: 'a'
value: '0'
```

**stringGreaterThanEquals**  
測試為 指定的字串是否`stringGreaterThanEquals`大於或等於 `value` 參數中指定的字串。例如：  

```
# Evaluates to true - 'a' is equal to 'A'
stringGreaterThanEquals: 'A'
value: 'a'

# Evaluates to true - 'b' is greater than 'a'
stringGreaterThanEquals: 'a'
value: 'b'

# Evaluates to true - 'a' is greater than '0'
stringGreaterThanEquals: '0'
value: 'a'

# Evaluates to false - '0' is less than 'a'
stringGreaterThanEquals: 'a'
value: '0'
```

**patternMatches**  
測試 `value` 參數中指定的字串是否符合為 指定的 regex 模式`patternMatches`。比較使用符合 RE2 語法的 [Golang regexp 套件](https://pkg.go.dev/regexp)。如需 RE2 規則的詳細資訊，請參閱 *GitHub* [中的 Google/re2](https://github.com/google/re2/wiki/Syntax) 儲存庫。  
下列範例顯示傳回 的模式比對`true`：  

```
patternMatches: '^[a-z]+$'
value: 'ThisIsValue'
```

## 比較數字
<a name="toe-compare-numbers"></a>

下列比較運算子使用數字。根據 YAML 規格，這些運算子提供的值必須是下列其中一種類型。對數值比較的支援使用 golang 大型套件比較運算子，例如：[func (\$1Float) Cmp](https://pkg.go.dev/math/big#Float.Cmp)。
+ Integer
+ 浮點數 （以 float64 為基礎，支援從 -1.7e\$1308 到 \$11.7e\$1308 的數字）
+ 符合下列 regex 模式的字串： `^[-+]?([0-9]+[.])?[0-9]+$`

**數字比較運算子**
+ [numberEquals](#numberEquals)
+ [numberLessThan](#numberLessThan)
+ [numberLessThanEquals](#numberLessThanEquals)
+ [numberGreaterThan](#numberGreaterThan)
+ [numberGreaterThanEquals](#numberGreaterThanEquals)

**numberEquals**  
測試 指定的數字是否`numberEquals`等於 `value` 參數中指定的數字。下列所有範例比較都會傳回 `true`：  

```
# Values provided as a positive number
numberEquals: 1
value: 1

# Comparison value provided as a string
numberEquals: '1'
value: 1

# Value provided as a string
numberEquals: 1
value: '1'

# Values provided as floats
numberEquals: 5.0
value: 5.0

# Values provided as a negative number
numberEquals: -1
value: -1
```

**numberLessThan**  
測試 指定的數字是否`numberLessThan`小於 `value` 參數中指定的數字。例如：  

```
# Evaluates to true
numberLessThan: 2
value: 1

# Evaluates to true
numberLessThan: 2
value: 1.9

# Evaluates to false
numberLessThan: 2
value: '2'
```

**numberLessThanEquals**  
測試 指定的數字是否`numberLessThanEquals`小於或等於 `value` 參數中指定的數字。例如：  

```
# Evaluates to true
numberLessThanEquals: 2
value: 1

# Evaluates to true
numberLessThanEquals: 2
value: 1.9

# Evaluates to true
numberLessThanEquals: 2
value: '2'

# Evaluates to false
numberLessThanEquals: 2
value: 2.1
```

**numberGreaterThan**  
測試 指定的數字是否`numberGreaterThan`大於 `value` 參數中指定的數字。例如：  

```
# Evaluates to true
numberGreaterThan: 1
value: 2

# Evaluates to true
numberGreaterThan: 1
value: 1.1

# Evaluates to false
numberGreaterThan: 1
value: '1'
```

**numberGreaterThanEquals**  
測試 指定的數字是否`numberGreaterThanEquals`大於或等於 `value` 參數中指定的數字。例如：  

```
# Evaluates to true
numberGreaterThanEquals: 1
value: 2

# Evaluates to true
numberGreaterThanEquals: 1
value: 1.1

# Evaluates to true
numberGreaterThanEquals: 1
value: '1'

# Evaluates to false
numberGreaterThanEquals: 1
value: 0.8
```

## 檢查檔案
<a name="toe-check-files"></a>

下列比較運算子會檢查檔案雜湊，或檢查檔案或資料夾是否存在。

**檔案和資料夾運算子**
+ [binaryExists](#binaryExists)
+ [fileExists](#fileExists)
+ [folderExists](#folderExists)
+ [fileMD5Equals](#fileMD5Equals)
+ [fileSHA1Equals](#fileSHA1Equals)
+ [fileSHA256Equals](#fileSHA256Equals)
+ [fileSHA512Equals](#fileSHA512Equals)

**binaryExists**  
測試應用程式是否可在目前路徑中使用。例如：  

```
binaryExists: 'foo'
```
在 Linux 和 macOS 系統上，對於名為 *foo* 的應用程式，其運作方式與下列 bash 命令相同：**type *foo* >/dev/null 2>&1**，其中 **\$1? == 0**表示成功比較。  
在 Windows 系統上，對於名為 *foo* 的應用程式，這的運作方式與 PowerShell 命令相同**& C:\$1Windows\$1System32\$1where.exe /Q *foo***，其中 **\$1LASTEXITCODE = 0**表示成功比較。

**fileExists**  
測試檔案是否存在於指定的路徑。您可以提供絕對或相對路徑。如果您指定的位置存在且為 檔案，則比較會評估為 `true`。例如：  

```
fileExists: '/path/to/file'
```
在 Linux 和 macOS 系統上，這的運作方式與下列 bash 命令相同：**-d */path/to/file***，其中 **\$1? == 0**表示成功比較。  
在 Windows 系統上，這的運作方式與 PowerShell 命令 相同**Test-Path -Path '*C:\$1path\$1to\$1file*' -PathType 'Leaf'**。

**folderExists**  
測試資料夾是否存在於指定的路徑。您可以提供絕對或相對路徑。如果您指定的位置存在且 是資料夾，則比較會評估為 `true`。例如：  

```
folderExists: '/path/to/folder'
```
在 Linux 和 macOS 系統上，這的運作方式與下列 bash 命令相同：**-d */path/to/folder***，其中 **\$1? == 0**表示成功比較。  
在 Windows 系統上，這的運作方式與 PowerShell 命令 相同**Test-Path -Path '*C:\$1path\$1to\$1folder*' -PathType 'Container'**。

**fileMD5Equals**  
測試檔案的 MD5 雜湊是否等於指定的值。例如：  

```
fileMD5Equals: '<MD5Hash>'
path: '/path/to/file'
```

**fileSHA1Equals**  
測試檔案的 SHA1 雜湊是否等於指定的值。例如：  

```
fileSHA1Equals: '<SHA1Hash>'
path: '/path/to/file'
```

**fileSHA256Equals**  
測試檔案的 SHA256 雜湊是否等於指定的值。例如：  

```
fileSHA256Equals: '<SHA256Hash>'
path: '/path/to/file'
```

**fileSHA512Equals**  
測試檔案的 SHA512 雜湊是否等於指定的值。例如：  

```
fileSHA512Equals: '<SHA512Hash>'
path: '/path/to/file'
```