Conditional constructs – if else

We use the if command to check the pattern or command status and accordingly we can make certain decisions to execute scripts or commands.

The syntax of the if conditional is as follows:

    if     command
    then
        command
        command
    fi

From the preceding syntax, we can clearly understand the working of the if conditional construct. Initially, if will execute the command. If the result of command execution is true or 0, then all the commands that are enclosed between then and fi will be executed. If the status of command execution after if is false or nonzero, then all the commands after then will be ignored and the control of execution will directly go to fi.

Let's learn another variation of if constructs.

Syntax:

 if command then ...

Get Learning Linux Shell Scripting now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.