■このサイトはPowerShell(MSH/Monad)奮闘記です
管理人「newpops(吉岡洋)」が「PowerShell(旧名:MSH/Monad)」の研究結果を日々綴っていきます。
【お知らせ】
この日記からPowerShellのTipsを抽出し「PowerShell FAQ」として整理しました。
2005-10-04
■[MSH]基本構文(14)
今さらという感じもしますが、条件分岐文の書き方です。
if文
$a = 10
if ($a -eq 10)
{
write-host True
}
else
{
write-host False
}
switch文
$a = 2
switch($a)
{
1 {"one"; break}
2 {"two"; break}
3 {"three"; break}
}
switch文(正規表現)
ルートディレクトリ以外にいる場合
MSH C:\temp> $a = get-location
MSH C:\temp> switch -regex ($a.path)
>> {
>> "(?<dir>[^\\]+$)" {write-host Current Directory is $matches.dir}
>> default {write-host In the root directory}
>> }
>>
Current Directory is temp
MSH C:\temp>
ルートディレクトリにいる場合
MSH C:\> $a = get-location
MSH C:\> switch -regex ($a.path)
>> {
>> "(?<dir>[^\\]+$)" {write-host Current Directory is $matches.dir}
>> default {write-host In the root directory}
>> }
>>
In the root directory
「?<aaa>」という正規表現の意味はまたの機会に。



