从 Windows cmd 脚本执行多个命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/197976/
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
Executing multiple commands from a Windows cmd script
提问by Darren Greaves
I'm trying to write a Windows cmd script to perform several tasks in series. However, it always stops after the first command in the script.
我正在尝试编写一个 Windows cmd 脚本来连续执行多个任务。但是,它总是在脚本中的第一个命令之后停止。
The command it stops after is a maven build (not sure if that's relevant).
它之后停止的命令是 Maven 构建(不确定这是否相关)。
How do I make it carry on and run each task in turn please?
请问如何让它依次执行和运行每个任务?
Installing any software or configuring the registry etc is completely out of the question - it has to work on a vanilla Windows XP installation I'm afraid.
安装任何软件或配置注册表等都是完全不可能的 - 恐怕它必须在普通的 Windows XP 安装上工作。
Ideally I'd like the script to abort if any of the commands failed, but that's a "nice to have", not essential.
理想情况下,如果任何命令失败,我希望脚本中止,但这是“很高兴”,不是必需的。
Thanks.
谢谢。
回答by Lou Franco
When you call another .bat file, I think you need "call" in front of the call:
当你调用另一个 .bat 文件时,我认为你需要在调用前“调用”:
call otherCommand.bat
回答by Steve
You can use the && symbol between commands to execute the second command only if the first succeeds. More info here http://commandwindows.com/command1.htm
只有在第一个命令成功时,您才能在命令之间使用 && 符号来执行第二个命令。更多信息在这里http://commandwindows.com/command1.htm
回答by Gulzar Nazim
Not sure why the first command is stopping. If you can make it parallel, you can try something like
不知道为什么第一个命令停止。如果你可以让它平行,你可以尝试像
start cmd.exe /C 1.bat
start cmd.exe /C 2.bat
回答by mhollander38
I have just been doing the exact same(ish) task of creating a batch script to run maven test scripts. The problem is that calling maven scrips with mvn clean install ... is itself a script and so needs to be done with call mvn clean install.
我刚刚做了完全相同(ish)的任务,即创建批处理脚本来运行 maven 测试脚本。问题是使用 mvn clean install 调用 maven 脚本......本身就是一个脚本,因此需要通过调用 mvn clean install 来完成。
Code that will work
有效的代码
rem run a maven clean install
cd C:\rbe-ui-test-suite
call mvn clean install
rem now run through all the test scripts
call mvn clean install -Prun-integration-tests -Dpattern=tc-login
call mvn clean install -Prun-integration-tests -Dpattern=login-1
Note rather the use of call. This will allow the use of consecutive maven scripts in the batch file.
请注意调用的使用。这将允许在批处理文件中使用连续的 Maven 脚本。
回答by JSON C11
Using double ampersands will run the second command, only if the first one succeeds:
使用双与号将运行第二个命令,仅当第一个成功时:
cd Desktop/project-directory && atom .
Where as, using only one ampersand will attempt to run both commands, even if the first fails:
其中,仅使用一个&符号将尝试运行这两个命令,即使第一个失败:
cd Desktop/project-directory & atom .
回答by Pulendar Vadde
If you are running in Windows you can use the following command.
如果您在 Windows 中运行,则可以使用以下命令。
Drive:
驾驶:
cd "Script location"
schtasks /run /tn "TASK1"
schtasks /run /tn "TASK2"
schtasks /run /tn "TASK3"
exit
回答by Mr Fooz
I don't know the direct answer to your question, but if you do a lot of these scripts, it might be worth learning a more powerful language like perl. Free implementations exist for Windows (e.g. activestate, cygwin). I've found it worth the initial effort for my own tasks.
我不知道你的问题的直接答案,但如果你做了很多这样的脚本,那么学习像 perl 这样更强大的语言可能是值得的。Windows 有免费的实现(例如 activestate、cygwin)。我发现为我自己的任务付出最初的努力是值得的。
Edit:
编辑:
As suggested by @Ferruccio, if you can't install extra software, consider vbscript and/or javascript. They're built into the Windows scripting host.
正如@Ferruccio 所建议的,如果您无法安装额外的软件,请考虑使用 vbscript 和/或 javascript。它们内置于 Windows 脚本主机中。
回答by Pushkar
Note that you don't need semicolons in batch files. And the reason why you need to use call is that mvn itself is a batch file and batch files need to call each other with call, otherwise control does not return to the caller.
请注意,批处理文件中不需要分号。而需要使用call的原因是mvn本身就是一个批处理文件,批处理文件之间需要通过call互相调用,否则控制权不会返回给调用者。