IF |
|
| Purpose: | Execute a single command if a condition is true. |
| Format: | IF [/I] condition command |
IF [/I] condition (command1) ELSE (command2)
condition |
|
command |
The command to execute if condition is TRUE. |
command1 |
The command to execute if condition is TRUE. |
command2 |
The command to execute if condition is FALSE. |
See also: Conditional expressions, IFF, @IF.
Usage:
IF is most often used only aliases and batch files. It is always followed by a condition (see Conditional expressions), and then a command. First condition is evaluated, and if it is TRUE, command is executed. Otherwise, command is ignored.
If the condition is FALSE, IF returns a non-zero result, so it can be evaluated by one of the conditional expression operators (II or &&).
The IF ... ELSE ... syntax of CMD.EXE is also supported:
IF [/I] condition (command1) ELSE (command2)
The commands to be executed must be enclosed in parentheses (as in a command group). If condition is TRUE, command1 is executed, if FALSE, command2 is executed. Note: this syntax is much less powerful than the IFF command, which is recommended.
When an IF test fails, the remainder of the command is discarded. Whether TCC continues with the next command on the line, or discards the rest of the line and goes to the next line is dependent upon the Duplicate CMD.EXE Bugs configuration option. CMD.EXE will discard all remaining commands on the line when an IF test fails, including those after a command separator or pipe character. If you do not want to reproduce CMD.EXE's behavior of an IF affecting all commands on a line, set DuplicateBugs to No in the .INI file.
For example, if Duplicate CMD.EXE Bugs is enabled (the default), the following command will display nothing, because the second ECHO command is discarded along with the first when the condition fails. If Duplicate CMD.EXE Bugs is disabled, it will display "hello":
|
[c:\] if 1 == 2 echo Wrong! & echo hello |
Option:
| /I | This option is included only for compatibility with CMD.EXE. It has no effect in TCC, since all string comparisons are case-insensitive unless you specify a case-sensitive test (EQC). |