View a markdown version of this page

在中使用比较运算符 EC2 TOE 组件文档 - EC2 Image Builder
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

在中使用比较运算符 EC2 TOE 组件文档

您可以结合使用以下比较运算符与 Assert 操作模块以及使用 if 构造的条件表达式。比较运算符可以对单个值进行比较,例如 stringIsEmpty,也可以将基准值与第二个值(变量值)进行比较,以确定条件表达式的计算结果是 true 还是 false

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

比较不同类型的值时,在比较之前可能会进行以下值转换:

  • 对于数值比较,如果变量值是字符串,则在计算之前将字符串 EC2 TOE 转换为数字。如果无法进行转换,则比较返回 false。例如,如果变量值为 "1.0",则可以进行转换,但如果变量值为 "a10",则转换将会失败。

  • 对于字符串比较,如果变量值是一个数字,则在计算之前将其 EC2 TOE 转换为字符串。

比较字符串

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

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'
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 参数中指定的字符串是否与为 patternMatches 指定的正则表达式模式匹配。比较使用符合 RE2 语法的 Golang regexp 包。有关 RE2 规则的更多信息,请参阅中的 google/re2 存储库。GitHub

以下示例显示了返回 true 的模式匹配:

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

比较数字

以下比较运算符适用于数字。根据 YAML 规范,为这些运算符提供的值必须是以下类型之一。对数字比较的支持使用 golang big 包比较运算符,例如:func (*Float) Cmp

  • 整数

  • Float(基于 float64,支持从 -1.7e+308 到 +1.7e+308 之间的数字)

  • 与以下正则表达式模式相匹配的字符串:^[-+]?([0-9]+[.])?[0-9]+$

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

检查文件

以下比较运算符检查文件哈希值或检查文件或文件夹是否存在。

binaryExists

测试应用程序在当前路径中是否可用。例如:

binaryExists: 'foo'
注意

在 Linux 和 macOS 系统上,对于名为的应用程序foo,其工作原理与以下 bash 命令相同:type foo >/dev/null 2>&1,其中$? == 0表示比较成功。

在 Windows 系统上,对于名为的应用程序foo,其工作原理与$LASTEXITCODE = 0表示成功比较& C:\Windows\System32\where.exe /Q foo的 PowerShell 命令相同。

fileExists

测试指定路径上是否存在文件。您可以提供绝对路径或相对路径。如果您指定的位置存在并且是一个文件,则比较的计算结果为 true。例如:

fileExists: '/path/to/file'
注意

在 Linux 和 macOS 系统上,其工作原理与以下 bash 命令相同:-d /path/to/file,其中 $? == 0 表示比较成功。

在 Windows 系统上,这与 PowerShell 命令的工作原理相同Test-Path -Path 'C:\path\to\file' -PathType 'Leaf'

folderExists

测试指定路径上是否存在文件夹。您可以提供绝对路径或相对路径。如果您指定的位置存在并且是一个文件夹,则比较的计算结果为 true。例如:

folderExists: '/path/to/folder'
注意

在 Linux 和 macOS 系统上,其工作原理与以下 bash 命令相同:-d /path/to/folder,其中 $? == 0 表示比较成功。

在 Windows 系统上,这与 PowerShell 命令的工作原理相同Test-Path -Path 'C:\path\to\folder' -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'