在 AWS TOE 元件文件中使用比較運算子 - EC2 Image Builder

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

在 AWS TOE 元件文件中使用比較運算子

您可以使用下列比較運算子搭配 宣告 動作模組,以及搭配使用 的條件式表達式如果建構。比較運算子可以在單一值上操作,例如 stringIsEmpty,也可以將基準值與第二個值 (變數值) 進行比較,以判斷條件式表達式是否評估為 truefalse

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

比較不同類型的值時,可能會在比較之前進行下列值轉換:

  • 對於數值比較,如果變數值是字串, 會在評估之前將字串 AWS TOE 轉換為數字。如果無法轉換,比較會傳回 false。例如,如果變數值為 "1.0",則轉換會運作,但如果變數值是"a10"轉換失敗。

  • 對於字串比較,如果變數值是數字, 會在評估之前將其 AWS TOE 轉換為字串。

比較字串

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

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。比較使用 與 Golang regexp 套件的 ,其符合 RE2 語法。如需 RE2 規則的詳細資訊,請參閱 GitHub 中的 Google/re2 儲存庫。

下列範例顯示傳回 的模式比對true

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

比較數字

下列比較運算子使用數字。根據 YAML 規格,為這些運算子提供的值必須是下列其中一種類型。對數值比較的支援使用 golang 大型套件比較運算子,例如 func (*Float) Cmp

  • Integer

  • 浮點數 (以 float64 為基礎,支援從 -1.7e+308 到 +1.7e+308 的數字)

  • 符合下列 regex 模式的字串: ^[-+]?([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 的應用程式,這的運作方式與 PowerShell 命令相同& C:\Windows\System32\where.exe /Q foo,其中 $LASTEXITCODE = 0表示成功比較。

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'