UDM - if Statement - Adding an Alternate Path with else Statement

Alternate Path without else Statement

Often there are occasions where you may want to take one branch if some condition is true and another branch if that condition is false, instead of merely picking up execution after the end statement. (Those lines would be executed if the condition was true as well, only after executing the statements inside the if-end pair.)

This could be accomplished by two well-phrased if statements, one following the other, as in this example:

if <$(_rc) GE 8>
  echo "There has been an error"
end
if <$(_rc) LT 8>
  echo "There has not been an error"
end


Flaws in this Methodology

However, while this is a perfectly valid method, it suffers from two potential flaws:

  1. You may find such logic difficult to read, thus making your UDM scripts more difficult to maintain, especially if you did not write them in the first place.
  2. If the comparison operation contains a variable and evaluates to true for the first comparison, it is possible something occurs in the statements inside the if-end pair that changes the value of the variable and makes the second comparison evaluate to true as well.

For example:

if <$(_lastrc) GE 8>
    echo "The last command was not successful"
end
if <$(_lastrc) LT 8>
    echo "The last command was successful"
end


In this example, if the command executed before the first if statement resulted in an error, the output would have been as follows:

Last command was not successful
Last command was successful

This is because the _lastrc variable holds the value of the last command executed by UDM. In the example given, the command executed before the first if statement resulted in an error (for example, result code = 8) and would result in the first if statement evaluating to true.

However, the successful execution of the print command inside the first if statement would result in _lastrc being set to 0, which would in turn mean the second if statement would evaluate to true, thus printing the second message. This would not have been what was intended.

In this contrived example, it is rather easy to see what went wrong and come up with a workaround: in this case, creating a new global variable into which to save the value of _lastrc - for example: set newvar=$(_lastrc) - and using the new variable in the comparison operations instead of _lastrc as its value would not be overwritten. For longer and more complex scripts, however, this may not be the case.

Alternate Path with else Statement

UDM offers an easy solution with the else statement. As part of the if statement, the else statement can be used to provide an alternative path to take if the comparison evaluates to false.

The general format of an if statement when an else statement is used with it is:

if expression
...
[else
...
end


In this if statement, the parameter for the statement is an expression. If the expression evaluates to a value that is not equal to zero, the positive branch is taken; otherwise the negative (else) branch is taken if one exists.

Examples

if <$(_rc) EQ 0>
    echo "Everything worked okay"
else
    echo "Something went wrong"
end

if <"$(myvar.exists)" EQ "yes">
     echo "The variable, myvar, has been defined."*
end


Note

The previous style of UDM if statements, shown in the following example, still is valid:

if <$(_lastrc) GE 8>
  print msg="The last command was not successful"
else
  print msg="The last command was successful"
end