Executing more than one command in Linux

There are various types of command execution methods available in both unix and linux and here are few of them which helps the administrators to run the commands with some rule-sets.

 

logical AND Using && with commands

 

If you want to execute the second command only if the first is successful. Use the following syntax.

 

  • # command1 && command2

 

command2 will be executed if command1 successfully completes (if command1 fails command2 won’t be run). This is called a logical AND and mostly used during the source package installation (iee.. make && make install).

 

logical OR using ||

 

If you want to execute the second command only if the first fails. Then use

 

  • # command1 || command2

 

Which will execute the command2 only if the command1 fails to execute. This type can be used during the backup or failover.

 

Executing Sequential commands using ;

 

If you want execute commands sequentially regardless of the success/failure of the previous you simply type

 

  • # command1; command2

You can also execute more than two command by adding another ; and the commands (iee.. cmd1; cmd2; cmd3; …cmdn)