whileループは、指定した条件が真の場合にループし続けます。
構文は下記の通りです。
while ( 条件 ){ 実行する文}
下記は変数$cntが10以下の場合ループし続けます。{ ~ }の中のコードブロックが実行される度に$cntがインクリメントされ、10以上になったときに条件を満たしループを終了します。
PS C:\Work> $cnt = 0 PS C:\Work> while ( $cnt -le 10 ) >> { >> Write-Host "Count: $cnt" >> $cnt++ >> } >> Count: 0 Count: 1 Count: 2 Count: 3 Count: 4 Count: 5 Count: 6 Count: 7 Count: 8 Count: 9 Count: 10
ここで1つ無限ループを行う方法を紹介します。
それは条件に 1 と記述することです。
PowerShellでは0は偽、それ以外は真と見なすため 1 記述すれば無限ループするというわけです。
下記は実行すると延々と現在日時を表示し続けます。
無限ループを停止するには[Ctrl] +[C]を入力してください。
PS C:\Work> while (1) >> { >> Write-Host (Get-Date) >> } >>
Theme design by Jelle Druyts
Pick a theme: BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2 BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2009, HIRO
E-mail