• No se han encontrado resultados

Grafica 4.Mortalidad por EDA Colombia semana 1-39 de 2002

2. PARASITOS VIGILANCIA CENTINELA

The do … while statement is one method for looping through code. The do … while statement evaluates the test condition before running the script block. If the condition is false, then it will not run. If however, it is true, the script will run the loop, evaluate the condition to see whether it is still true, and then run again.

In the CountDownTimer.ps1 script, a do … while loop is used to alternatively pause the script and display the current time as the script counts down to a specified time just like an alarm clock. The script begins by using the variable $dtmTime to hold the object that is returned by the Get-Date cmdlet. When you supply the values for hour, minute, and second, the Get-Date cmdlet will return a dateTime object representing the specified time. The do … while loop eval- uates the current time (retrieved by the Get-Date cmdlet and stored in the $dtmCurrent vari- able) with the time specified in the $dtmTime variable.

If the $dtmCurrent time value is less than the time specified in $dtmTime, then the script will print out the current time value that is contained in the $dtmCurrent variable. To do this, we use the -lt comparison operator. Other comparison operators are seen in Table 4-6. The Start-Sleep cmdlet is used to pause script execution for 2 seconds (as indicated by the -s 2 argument). After the while condition has been satisfied, the script will print out the message “time reached”, and it uses the`a special escape sequence to play the alert beep. The CountDownTimer.ps1 script is shown here:

CountDownTimer.ps1

$dtmTime = get-date -h 04 -mi 23 -s 00 do {$dtmCurrent = Get-Date -DisplayHint time "The current time is " + $dtmCurrent

"counting to " + $dtmtime start-sleep -s 2

} while ($dtmCurrent -lt $dtmTime) "time reached `a"

Quick Check

Q. If you want to perform an operation as long as a value is equal to a certain number, what operator do you use?

A. If you want to perform an operation as long as a value is equal to a certain number, use the -eq operator.

Q. If you want to sound a beep in a script, what special escape character can you use?

A. If you want to sound a beep in a script, use the `a special escape character.

Using Do … Until

The do … until statement provides a means of looping through your code until a condition becomes true. The difference between do … while and do … until is that the do … until state- ment evaluates at the end of the loop. This means the code will always run at least once. A basic do … until command is shown here. The value of $i is set to 10. The semicolon is then used to separate the value assignment from do … loop. The curly brackets separate the script block from the rest of the code. Inside the script block, 1 is subtracted from the value of $i each time we loop through the code. The double hyphen (--) operator is used to do the sub- traction. A string is used to print out a status message that lets us know the current value of $i. The until block tells the command to continue to run until the value of $i is equal to 0. $i = 10; do {$i --; "i is $i"} until ($i -eq 0)

Table 4-6 Comparison Operators

Operator Description

-eq equals

-ne not equal

-gt greater than

-ge greater than or equal to

-lt less than

-le less than or equal to

-like wild card comparison

-notlike wild card comparison

-match regular expression comparison

In the ReadTxtFile.ps1 script, the variable $i is given a value of 0. The value of $i is then incre- mented after the script displays one line of text from the text file. The until clause evaluates whether the value of $i is equal to the length of the text file. When the length of the text file is the same as the value of $i, then we have reached the end of the file.

ReadTxtFile.Ps1

$strTxtFile = "c:\mytest\loopbackprocesses.txt" $i = 0

$mytext = Get-Content $strTxtFile do {

$mytext[$i] $i ++

} until ($i -eq $mytext.length)

The exploring the do ... until procedure will highlight this behavior.

Exploring the do … until statement 1. Open Windows PowerShell.

2. Type the variable $i and assign the value of 0 to it. Use a semicolon to separate this vari-

able assignment from the following command. This code is shown here: $i=0;

3. Open a do loop and add one to the $i variable. Use the special double plus symbol (++)

operator. Include the semicolon command separator. This code is shown here: do {$i++;

4. Print out the string "I is equal to" and include the value of $i. Close out the script block.

This code is shown here: "i is equal to $i"}

5. Add the until clause and evaluate the value of $i when it is equal to 0. This code is shown here:

until ($i -eq 0)

6. Before you run this command, realize it will go into a continuous loop. This is because do … until evaluates at the end of the script. The value of $i will have already been incremented

to 1 before the evaluation being performed. You can use ^c to break into the loop.

7. Run the command. After a few lines have scrolled past, use ^c to break into the loop. 8. Press the up arrow to retrieve the previous command. This line of code is shown here:

$i=0;do {$i++; "i is equal to $i"} until ($i -eq 0)

9. Convert the do … until loop into a do … while loop. To do this, simply replace the word until with while. The altered code is shown here:

10. This code runs one time and produces the output shown here:

i is equal to 1

11. This concludes this procedure. Commands used are stored in the ExploringDoLoop.txt file.

Documento similar