Introduction to conditional statement (If)
Sometimes, you need to take different action depending on the success or failure of a command. The if statement helps you in such conditions.
Syntax
The condition is true or returns 0 (zero), the statements get executed.NOTE:
-
There should be a space with in square brackets at starting and ending.
Picture courtesy: https://kodekloud.com/
Example
if [[ 5 -gt 0 ]]
-
The condition often involves numerical or string comparison tests.
- The condition returns a status of zero when it succeeds and some other status when it fails.
- The condition may be a command (e.g
if [ $(whoami) = "root" ]
) - Unary expressions are often used to examine the status of a file