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
Operator
Description
!EXPRESSION
The EXPRESSION is false
-n STRING
STRING length is greater than zero
-z STRING
The length of STRING is zero (empty)
STRING1 = STRING 2
STRING1 is equal to STRING2
STRING1 != STRING 2
STRING1 is not equal to STRING2
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
INTEGER1 -gt INTEGER2
INTEGER1 is greater then INTEGER2
INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2
-d FILE
FILE exists and is a Directory
-e FILE
FILE exists
-r FILE
FILE exists and has read permission
-s FILE
FILE exists and is not empty
-w FILE
FILE exists and has write permission
-x FILE
FILE exists and has execute permission
Last updated
Was this helpful?