#navi(../)
* PowerShellで数値の比較を行う比較演算子の一覧 [#m9a1947b]
PowerShellでif文やwhile文などで使用する条件比較で数値比較する場合は~
以下に記す比較演算子を使用します。

#contents
#htmlinsertpcsp(win-top.html,win-sp.html)

* 関連記事 [#z7c62dfc]
-[[PowerShellの比較演算子一覧>PowerShell/PowerShellの比較演算子一覧]]
-[[PowerShellで数値の比較を行う比較演算子の一覧>PowerShell/数値の比較を行う比較演算子の一覧]]
-[[PowerShellのif文でand,or,xor,notを記述する方法>PowerShell/PowerShellのif文でand,or,xor,notを記述する方法]]

* 比較演算子 [#f56159a7]
下表に記す文字列により、数値比較ができます。
|-eq|等しいかをチェック 数値 = 数値|''eq''ual|
|-ne|異なるかをチェック 数値 ≠ 数値|''n''ot ''e''qual|
|-lt|数値 < 数値をチェック|''l''ess ''t''han|
|-le|数値 ≦ 数値をチェック|''l''ess than or ''e''qual|
|-gt|数値 > 数値をチェック|''g''reater ''t''han|
|-ge|数値 ≧ 数値をチェック|''g''reater than or ''e''qual|

* 比較演算子のサンプルスクリプト [#e3e17e17]
PowerShellで比較演算子を使ったサンプルスクリプトを以下に記します。~
使用方法は以下の通りです。
 .\hikaku.ps1 値 値
#ref(hikaku.ps1)
 $this = $MyInvocation.MyCommand.Name
 
 function Usage {
       Write-Host Usage : $this '<value> <value>'
       Exit 1
 }
 
 if ($Args.Length -ne 2) {
       Usage
 }
 
 $a = $Args[0]
 $b = $Args[1]
 
 # -eq : equal
 if($a -eq $b) {
     Write-Host "$a = $b"
 }
 
 # -ne : not equal
 if($a -ne $b) {
     Write-Host "$a != $b"
 }
 
 # -lt : less than
 if($a -lt $b) {
     Write-Host "$a < $b"
 }
 
 # -le : less than or equal
 if($a -le $b) {
     Write-Host "$a <= $b"
 }
 
 # -gt : greater than
 if($a -gt $b) {
     Write-Host "$a > $b"
 }
 
 # -ge : greater than or equal
 if($a -ge $b) {
     Write-Host "$a >= $b"
 }

以上、PowerShellの数値の比較演算子についてでした。

#htmlinsertpcsp(win-btm.html,win-sp.html)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS