#眉標=PowerShell #副標=PowerShell = Shell + Script + .NET(6) #大標= PowerShell的流程控制 #作者=文/蔡學鏞 ==<反灰>=========== $l = 0; foreach ($f in dir *.txt) { $l += $f.length } ================ ==<反灰>=========== PS > foreach ($i in 1..10) >> { [void] $foreach.MoveNext(); $i + $foreach.current } >> 3 7 11 15 19 ================ ==<反灰>=========== PS > foreach ($i in "hi") {$i } hi ================ ==<反灰>=========== :outer while (1) { while(1) { break outer; } } ================ ==<反灰>=========== PS > switch (1) { 1 { "One" } 2 { "two" } } One ================ ==<反灰>=========== PS > switch (2) { 1 { "One" } 2 { "two" } 2 {"another 2"} } two another 2 ================ ==<反灰>=========== PS > switch (2) {1 {"One"} 2 {"two"; break} 2 {"another 2"}} two ================ ==<反灰>=========== PS > switch (3) { 1 { "One" } 2 { "two" } default {"default"} } default ================ ==<反灰>=========== PS > switch -wildcard ('abc') {a* {"astar"} *c {"starc"}} astar starc ================ ==<反灰>=========== PS > switch -regex ('abc') {^a {"a*: $_"} 'c$' {"*c: $_"}} a*: abc *c: abc ================ ==<反灰>=========== PS > switch (8) { >> {$_ -gt 3} {"greater than three"} >> {$_ -gt 7} {"greater than 7"}} >> greater than three greater than 7 ================ ==<反灰>=========== PS > switch(1,2,3,4,5,6) { >> {$_ % 2} {"Odd $_"; continue} >> 4 {"FOUR"} >> default {"Even $_"} >> } >> Odd 1 Even 2 Odd 3 FOUR Odd 5 Even 6 ================ ==<反灰>=========== PS > foreach ($f in dir *.txt) { $f.length } ================ ==<反灰>=========== PS > dir *.txt | foreach-object {$_.length} ================ ==<反灰>=========== PS > 1..5|%{$_*2} ================ ==<反灰>=========== PS > 1..10 | where {! ($_ -band 1)} ================ ==<反灰>=========== PS > $result = new-object System.Collections.ArrayList PS > for ($i=1; $i -le 10; $i++) { $result.Append($i) } PS > "$($result.ToArray())" 1 2 3 4 5 6 7 8 9 10 ================ ==<反灰>=========== PS > $result = $(for ($i=1; $i -le 10; $i++) {$i}) PS > "$result" 1 2 3 4 5 6 7 8 9 10 ================ ==<反灰>=========== PS > $var ================ ==<反灰>=========== PS > $var = $(if (! $var) { 12 } else {$var}) PS > $var 12 ================ ==<反灰>=========== PS > $var="Hello there" PS > $var = $(if (! $var) { 12 } else {$var}) PS > $var Hello there ================