Archive for June 13, 2010

Now that you know how to work with variables and form expressions, let’s look at something more advanced: selection statements used with the command line. When you want to control the flow of execution based upon conditions known only at run time, you’ll use

  • if to execute a statement when a condition is true, such as if the operating system is Windows 2000 or later. Otherwise, the statement is bypassed.
  • if not to execute a statement when a condition is false, such as if a system doesn’t have a C:\Windows directory. Otherwise, the statement is bypassed.
  • if…else to execute a statement if a condition is matched (true or false) and to otherwise execute the second statement.

Although some of the previous examples in this chapter have used conditional execution, we haven’t discussed the syntax for these statements or the associated comparison operators. If your background doesn’t include programming, you probably will be surprised by the power and flexibility of these statements.

Using If

The if statement is used for conditional branching. It can be used to route script execution through two different paths. Its basic syntax is

if condition (statement1) [else (statement2)]

Here each statement can be a single command or multiple commands chained, piped, or grouped within parentheses. The condition is any expression that returns a Boolean value of True or False when evaluated. The else clause is optional, meaning you can also use the syntax

if condition (statement)
Tip Technically, parentheses aren’t required, but using them is a good idea, especially if the condition includes an echo statement or a command with parameters. If you don’t use parentheses in these instances, everything that follows the statement on the current line will be interpreted as part of the statement, which usually results in an error.

The if statement works like this: If the condition is true, then statement1 is executed. Otherwise statement2 is executed (if it is provided). In no case will both the if and the else clauses be executed. Consider the following example:

if "%1"=="1" (echo is one) else (echo is not one)

Here if the first parameter passed to the script is 1, then “is one” is written to the output. Otherwise, “is not one” is written to the output.

The command shell expects only one statement after each condition. Typically, the statement is a single command to execute. If you want to execute multiple commands, you’ll need to use one of the command piping, chaining, or group techniques, as in this example:

if "%1"=="1" (hostname & ver & ipconfig /all) else (netstat -a)

Here all three commands between parentheses will execute if the first parameter value is 1.

Using If Not

When you want to execute a statement only if a condition is false, you can use if not. The basic syntax is

if not condition (statement1) [else (statement2)]

Here the command shell evaluates the condition. If it is false, the command shell executes the statement. Otherwise, the command doesn’t execute and the command shell proceeds to the next statement. The else clause is optional, meaning you can also use the syntax

if not condition (statement1)

Consider the following example:

if not errorlevel 0 (echo An error has occurred!) & (goto :EXIT)

Here you check for error conditions other than zero. If no error has occurred (meaning the error level is zero), the command shell continues to the next statement. Otherwise, the command shell writes “An error has occurred!” to the output and exits the script. (You’ll learn all about goto and subroutines later in the chapter.)

Using If Defined and If Not Defined

The final types of if statements you can use are if defined and if not defined. These statements are designed to help you check for the existence of variables, and their respective syntaxes are

if defined variable statement

and

if not defined variable statement

Both statements are useful in your shell scripts. In the first case, you execute a command if the specified variable exists. In the second case, you execute a command if the specified variable does not exist. Consider the following example:

if defined numServers (echo Servers: %numServers%)

Here, if the numServers variable is defined, the script writes output. Otherwise, the script continues to the next statement.

Nesting Ifs

A nested if is an if statement within an if statement. Nested ifs are very common in programming, and command-shell programming is no exception. When you nest if statements, pay attention to the following points:

  1. Use parentheses to define blocks of code and the @ symbol to designate the start of the nested if statement.
  2. Remember that an else statement always refers to the nearest if statement that is within the same block as the else statement and that is not already associated with another else statement.

Here is an example:

if "%1"=="1" (
@if "%2"=="2" (hostname & ver) else (ver)) else (hostname & ver & 
netstat -a)

The first else statement is associated with if “%2″==”2”. The final else statement is associated with if “%1″==”1”.

Making Comparisons in If Statements

Frequently, the expression used to control if statements will involve comparison operators as shown in previous examples. The most basic type of string comparison is when you compare two strings using the equality operator (=), such as

if stringA==stringB statement

Here, you are performing a literal comparison of the strings and if they are exactly identical, the command statement is executed. This syntax works for literal strings but is not ideal for scripts. Parameters and arguments may contain spaces or there may be no value at all for a variable. In this case, you may get an error if you perform literal comparisons. Instead, use double quotation marks to perform a string comparison and prevent most errors, such as

if "%varA%"=="%varB%" statement

or

if "%varA%"=="string" statement

String comparisons are always case-sensitive unless you specify otherwise with the /i switch. The /i switch tells the command shell to ignore the case in the comparison, and you can use it as follows:

if /I "%1"=="a" (echo A) else (echo is not A)

These operators are used in place of the standard equality operator, such as

if "%varA%" equ "%varB" (echo The values match!)
Advertisements

Group Command Sequences

Posted: June 13, 2010 in Commands, System Information
Tags: ,


When you combine multiple commands, you may need a way to group commands to prevent conflicts or to ensure that an exact order is followed. You group commands using a set of parentheses. To understand why grouping may be needed, consider the following example. Here, you want to write the host name, IP configuration, and network status to a file, so you use this statement:

hostname & ipconfig & netstat -a > current-config.log

When you examine the log file, however, you find that it contains only the network status. The reason for this is that the command line executes the commands in sequence as follows:

  1. hostname
  2. ipconfig
  3. netstat – a > current_config.log

Because the commands are executed in sequence, the system host name and IP configuration are written to the command line, and only the network status is written to the log file. To write the output of all the commands to the file, you would need to group the commands as follows:

(hostname & ipconfig & netstat -a) > current_config.log

Here, the output of all three commands is collected and then redirected to the log file. You can also use grouping with conditional success and failure. In the following example, both Command1 and Command2 must succeed for Command3 to execute:

(cd C:\working\data & xcopy n:\docs\*.*) && (hostname >
n:\runninglog.txt)

Hey Guys……

Download the MCTS 70-236 Exchange Server 2007  E book in the below link……

Please let me know if you would like to have any different Books

DownLoad Here