Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run command2 only if command1 succeeded in cmd windows shell

How do we combine commands in cmd shell language such that the second command is only executed if the first command successfully completed?

something like following bash-command

make && ./a.out

a.out is only executed if make was successful

like image 804
user674669 Avatar asked Sep 13 '11 13:09

user674669


People also ask

Which of the following will run command2 only if command1 is successful?

Using a single ampersand (&) will cause the first command and then the second command to be run in sequence. Using double ampersands (&&) introduces error checking. The second command will run only if the first command is successful.

When used to separate commands what will execute the second command only if the first command fails to complete?

Just use & instead of && . The second command will be executed even if the first one fails.


2 Answers

The following

command1 && command2

should work on cmd as well. Quote from here:

When using cmd.exe, you can put multiple commands on the same line by using ‘&’ or ‘&&’ between commands. Using a single ampersand (&) will cause the first command and then the second command to be run in sequence. Using double ampersands (&&) introduces error checking. The second command will run only if the first command is successful.

like image 98
aioobe Avatar answered Oct 14 '22 05:10

aioobe


An AND list has the form

command1 && command2

command2 is executed if, and only if, command1 returns an exit status of zero.

An OR list has the form

command1 || command2

command2 is executed if and only if command1 returns a non-zero exit status. The return status of AND and OR lists is the exit status of the last command executed in the list.

like image 44
Fredrik Pihl Avatar answered Oct 14 '22 06:10

Fredrik Pihl