Command-Line Selection Statements
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...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)
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:
-
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)
To perform more advanced equality tests, you’ll need to use the
comparison operators shown in Table
3-3. These operators are used in place of the standard equality operator,
such as
if "%varA%" equ "%varB" (echo The values match!)
Operator
|
Description
|
---|---|
equ
|
Checks for equality and evaluates to true if the values are
equal
|
neq
|
Checks for inequality and evaluates to true if the values
are not equal
|
lss
|
Checks for less than condition and evaluates to true if value1 is less than value2
|
leq
|
Checks for less than or equal to condition, and evaluates to
true if value1 is less than or equal to value2
|
gtr
|
Checks for greater than condition and evaluates to true if
value1 is greater than value2
|
geq
|
Checks for greater than or equal to condition, and evaluates
to true if value1 is greater than or equal to value2
|
No comments:
Post a Comment