IF ELSE in a Windows shell

When in Rome, do as the Romans do, they say. Same when in Windows. This tiny post is about the quirky syntax of the IF ELSE statement in a batch file for that environment.

But at least a minimal background is required.

The file extension

In the old DOS times, a batch file was identified by the .BAT extension, nowadays is more common to see a .CMD extension. If your script is designed to run on a modern system you could treat these two extensions as synonim.

But if you want to stay on the safe side, and remark that your shell file is not designed to run on DOS, you should prefer for the latter.

Environment variable

If the IF ELSE command is quirky, the behavior of SET, the statement caring of environment variables management, is a good simple way to get an headache. Just a fast introduction to it, here.

You just call SET to print the complete list of all the environment variables currently available. To create, or reset an existing environment variable, you use call something like this:
SET USER=Tom
In this case we are saying that USER (all uppercase) should be set to Tom. The SET command is very sensitive to blanks, and allow you to create different variables named in weird ways, with blanks, upper and lower case letters (this is actually a feature, that was not supported by the old DOS), and even in the associated value blanks could be quite a surprise, if you put them at the beginning of it. So remember: no blanks near the equal sign in a SET statement, at least if you would like to have a quiet life.

Passing parameters

This is a far to be perfect mechanism but, for what it cares here, it could be explained very easily. They are seen as variables ranging from 0 (the name used to call the script itself), than 1 (first parameter) to 9 (ninth parameter). What if our script needs more than 9 parameters? (Hint: we can use SHIFT).

IF ELSE

The major nuisance in the IF ELSE construct for Windows batch files is due to the intepreter nature's, that read and execute one line after the other. That means any line should be executable atomically, and just a minimal support to complex instruction structure is provided.

So, we have just two alternatives when writing an IF ELSE: writing the complete statement on a single line, of using a very strict structure.

I guess an example is the better way of showing what I mean.
@ECHO OFF

IF "%1" == "" (ECHO Welcome, stranger!) ELSE ( ECHO Hello, %1! )

IF NOT DEFINED USER (
   ECHO no local user was defined
   SET USER=%1
) ELSE (
   ECHO local user was %USER%
   set USER=
)
This is a little silly batch application, but it has something interesting to say.

The first IF ELSE is a one-liner. It checks the first parameter for being actually something and, if so, greets the caller using it. Notice the double quotes around %1 in the test clause to let us compare its value against the empty string. There are other more sophisticated way to get the same result, but here it suffices.

The second IF ELSE is more complicated, so complicated that we used the multi-line syntax, a real pain. You must keep this format, otherwise you get mystifying syntax errors.

It checks if the env variable USER is undefined, in that case we print a message and set it to the parameter passed by the caller. Naturally, if the user didn't pass anything, the result is to remove USER from the environment variables.

The ELSE block prints the current value of USER (notice that it is decorated with percent signs to solve the ambiguity between the plain word USER and the variable name USER), and then remove it from the environment.

No comments:

Post a Comment