Posted on

If..else..fi – Linux Shell Scripting Tutorial – A Beginner’s handbook

if..else..fi allows to make choice based on the success or failure of a command. For example, find out if file exists (true condition) or not (false condition) and take action based on a condition result.

           if test var -eq val
           then
                       command executed successfully
                       execute all commands up to else statement
                       or to fi if there is no else statement

           else
                       if command failed then
                       execute all commands up to fi
           fi

Source: If..else..fi – Linux Shell Scripting Tutorial – A Beginner’s handbook