if, else, and elif
We can write scripts that can perform certain actions depending on conditions using if
, else
and elif
. The syntax for these things is quite particular so you need to be careful. At a high level, we are looking at...
Simple if else Script
The easiest way to show this in action is to examine a working script with an if
statement in it:
Simple if, elif, else Script
The above example only allows for 2 execution branches; one if a condition is true, or the other if not. This is fine if your script is fairly simple, however this can be rather limiting. So, we can add the elif
statement to add further conditional checks, providing more granularity to the tool:
Again, let's examine a working example:
In both scripts, you are seeing operators such as -lt
and -gt
, which are short for Less Than and Greater Than. The table below contains a comprehensive list of operators you can use in your scripts
Operators
Last updated