Flylib.com

Books Software

 
 
 

Section A.5. Global Variables


A.5. Global Variables

A set of global variables is defined by the shell at startup for convenience (see Table A-10).

Table A-10. Default global variables

Variable

Value

$home

User's home directory

$host

Information about the MSH runtime

$mshhome

Path of currently running msh.exe

$pid

Process ID of current msh.exe process

$pwd

Current working directory




A.6. Preference Variables

MSH uses several variables to hold the default values to be used when a ubiquitous parameter is not explicitly set on a cmdlet. For cmdlets that respect the ubiquitous parameters, the values of these variables will be used unless explicitly overridden on invocation. See Table A-11 for a list of preference variables.

Table A-11. Preference variables

Variable

Meaning

$ConfirmPreference

Defines the default -Confirm setting

$DebugPreference

Defines the default -Debug setting

$ErrorActionPreference

Defines the default -ErrorAction setting

$VerbosePreference

Defines the default -Verbose setting

$WhatIfPreference

Defines the default -WhatIf setting




A.7. Execution Flow

MSH offers two mechanisms for controlling the flow of script execution: the if statement and the switch statement.

A.7.1. if Statement

if (


<test 1>


) {


<block 1>


}
elseif (


<test2>


) {


<block 2>


}
...
else {


<block 3>


}

As soon as one of the tests evaluates to true, the corresponding block is executed and no other tests are performed. The elseif and else parts of the statement are optional.

A.7.2. switch Statement

switch (


<expression>


)
{


<value>


{


<block 1>


}             # case 1
    {


<test>


} {


<block 2>


}            # case 2
    default {


<block 3>


}             # case 3
}

In case 1, MSH performs a test on the expression and value and, if they are equal, executes the first block. In case 2, MSH evaluates the test (in which $_ is used for the value of <expression> ), and if the test evaluates to true, the second block is run. There may be many instances of these two cases, each using a different value or test. The default case (case 3) may only occur once and its associated block is run only if none of the other cases results in a match. Switch statements do not terminate after a match is made; instead, the blocks associated with all matching tests are run.



A.8. Loops

While looping is a general concept, MSH offers three specific mechanisms for performing iterative tasks : the while loop, the for loop, and the foreach loop.

A.8.1. while Loop

while (


<condition>


)
{


<block>


}

The while loop is the simplest of loops and will repeatedly execute a block until a condition no longer holds true. The script block must at some point break the condition or there must be an external expectation that the condition will cease to hold at some point in the future; otherwise , the loop will continue indefinitely.

A.8.2. for Loop

for (


<initialization>


;


<condition>


;


<repeat command>


)
{


<block>


}

The for loop gives complete control of the start, middle, and end of the loop. Like while , the condition is tested before each run, and the block is executed if a condition holds true. Additionally, a for statement can include an initialization statement that is run once (for example, to set a counter to zero). The repeat command is also optional (but recommended!) and is executed after each run through the block.

A.8.3. foreach Loop

foreach (


<element>


in


<elements>


)
{


<block>


}

The foreach loop is well suited to processes in a collection such as an array or a sequence of objects emitted from a cmdlet. The block is executed once for each object in the elements list. During each invocation, the variable named in the <element> part is populated with the current object.

The foreach language element (described here) is different than the foreach-object cmdlet (which is also aliased as foreach ). The foreach-object cmdlet is designed for pipeline use and does not take a ( <element> in <elements> ) term .