bash 仅当 command1 在 cmd windows shell 中成功时才运行 command2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7402587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:01:29  来源:igfitidea点击:

Run command2 only if command1 succeeded in cmd windows shell

bashshellcmdcommand-prompt

提问by user674669

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

我们如何在 cmd shell 语言中组合命令,以便仅在第一个命令成功完成后才执行第二个命令?

something like following bash-command

类似于以下 bash 命令

make && ./a.out

a.outis only executed if makewas successful

a.outmake在成功时执行

回答by aioobe

The following

下列

command1 && command2

should work on cmdas well. Quote from here:

也应该继续工作cmd。从这里引用:

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.

使用 cmd.exe 时,可以通过在命令之间使用“&”或“&&”将多个命令放在同一行。使用单个与号 (&) 将导致按顺序运行第一个命令和第二个命令。使用双与号 (&&) 会引入错误检查。 只有当第一个命令成功时,第二个命令才会运行

回答by Fredrik Pihl

An AND list has the form

AND 列表具有以下形式

command1 && command2

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

当且仅当 command1 返回退出状态为零时才执行 command2。

An OR list has the form

OR 列表具有以下形式

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.

当且仅当 command1 返回非零退出状态时才执行 command2。AND 和 OR 列表的返回状态是列表中执行的最后一个命令的退出状态。