Conditional Program or Command Execution

Conditional program or commands execution i.e. && and ||


The control operators in conditional execution are && and || .
The syntax for && is as follows :

           cmd1 && cmd2
           cmd2 will execute if only cmd1 returns an exit status of zero.

The syntax for OR list as follows
Syntax:
           cmd1 || cmd2
           cmd2 is executed if and only if cmd1 returns a non-zero exit status.

We can use both as follows syntax:



cmd1 && cmd2 if exist status is zero || cmd3 if exit status is non-zero
if cmd1 will get executed successfully then the shell will run cmd2 and if cmd1 is not successful then the cmd3 will be executed.

Example:


           $ rm myfile && echo "File removed successfully" || echo "File not removed"

If file "myfile" removed successfull exit status will be zero then "echo File removed successfully" statement will executed, otherwise "echo File not removed" statement will be executed since "exist status is non-zero"

Output : 


0 Comments:

Post a Comment