

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在 AWSTOE 组件文档中使用比较运算符
<a name="toe-comparison-operators"></a>

您可以结合使用以下比较运算符与 **[Assert](toe-action-modules.md#action-modules-assertion)** 操作模块以及使用 [if 构造语法](toe-conditional-constructs.md#toe-conditional-if)的条件表达式。比较运算符可以对单个值进行比较，例如 `stringIsEmpty`，也可以将基准值与第二个值（变量值）进行比较，以确定条件表达式的计算结果是 `true` 还是 `false`。

如果对两个值进行比较，则第二个值可以是链式变量。

比较不同类型的值时，在比较之前可能会进行以下值转换：
+ 对于数值比较，如果变量值是字符串，则在计算之前将字符串 AWSTOE 转换为数字。如果无法进行转换，则比较返回 `false`。例如，如果变量值为 `"1.0"`，则可以进行转换，但如果变量值为 `"a10"`，则转换将会失败。
+ 对于字符串比较，如果变量值是一个数字，则在求值之前将其 AWSTOE 转换为字符串。

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

以下比较运算符结合字符串来比较值、测试空格或空字符串，或者将输入值与正则表达式模式进行比较。字符串比较不区分大小写，也不会从字符串输入的开头或结尾去掉空格。

**字符串比较运算符**
+ [stringIsEmpty](#stringIsEmpty)
+ [stringIsWhitespace](#stringIsWhitespace)
+ [stringEquals](#stringEquals)
+ [stringLessThan](#stringLessThan)
+ [stringLessThanEquals](#stringLessThanEquals)
+ [stringGreaterThan](#stringGreaterThan)
+ [stringGreaterThanEquals](#stringGreaterThanEquals)
+ [patternMatches](#patternMatches)

**stringIsEmpty**  
如果指定的字符串不包含任何字符，则 `stringIsEmpty` 运算符将返回 `true`。例如：  

```
# 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'
```

**stringLessThan等于**  
测试为 `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'
```

**stringGreaterThan等于**  
测试为 `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` 参数中指定的字符串是否与为 `patternMatches` 指定的正则表达式模式匹配。比较使用符合语法的 [Golang regexp 包](https://pkg.go.dev/regexp)。 RE2 有关 RE2 规则的更多信息，请参阅中的 [google/re2](https://github.com/google/re2/wiki/Syntax) 存储库。*GitHub*  
以下示例显示了返回 `true` 的模式匹配：  

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

## 比较数字
<a name="toe-compare-numbers"></a>

以下比较运算符适用于数字。根据 YAML 规范，为这些运算符提供的值必须是以下类型之一。对数字比较的支持使用 golang big 包比较运算符，例如：[func (\$1Float) Cmp](https://pkg.go.dev/math/big#Float.Cmp)。
+ 整数
+ Float（基于 float64，支持从 -1.7e\$1308 到 \$11.7e\$1308 之间的数字）
+ 与以下正则表达式模式相匹配的字符串：`^[-+]?([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'
```

**numberLessThan等于**  
测试为 `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'
```

**numberGreaterThan等于**  
测试为 `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*，其工作原理与**\$1LASTEXITCODE = 0**表示成功比较**& C:\$1Windows\$1System32\$1where.exe /Q *foo***的 PowerShell 命令相同。

**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'**。

**文件MD5等于**  
测试文件的 MD5 哈希值是否等于指定值。例如：  

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

**文件SHA1等于**  
测试文件的 SHA1 哈希值是否等于指定值。例如：  

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

**文件SHA256等于**  
测试文件的 SHA256 哈希值是否等于指定值。例如：  

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

**文件SHA512等于**  
测试文件的 SHA512 哈希值是否等于指定值。例如：  

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