Skip to content

"if" Code Snippet

If statement format & examples

Here is a quick reference showing both the format of an ":if" statement and code examples.

#Format:
:if (<condition>) do={
    <command>;
    <command>;
    ...
}

# Alternate if-else format:
:if (<condition>) do={
    <command>;
    <command>;
    ...
} else={
    <command>;
    <command>;
    ...
}

RouterOS :if statement example:

# "if" statement
:local TimeNow [/system clock get time];

# additional greeting if before noon
:if ($TimeNow < 12:00:00) do={
    :put "Good morning.";
}

:put "Have a good day!";
# "if-else" statement
:local TimeNow [/system clock get time];

:if ($TimeNow < 12:00:00) do={
    :put "Good morning.";
} else={
    :put "Good afternoon/evening."
}

:put "Have a good day!";

Buy

Bookcover