简体   繁体   English

从Windows cmd脚本执行多个命令

[英]Executing multiple commands from a Windows cmd script

I'm trying to write a Windows cmd script to perform several tasks in series. 我正在尝试编写一个Windows cmd脚本来执行多个串行任务。 However, it always stops after the first command in the script. 但是,它始终在脚本中的第一个命令后停止。

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. 谢谢。

当你打电话给另一个.bat文件时,我认为你需要在通话前“打电话”:

call otherCommand.bat

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

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

I have just been doing the exact same(ish) task of creating a batch script to run maven test scripts. 我刚刚完成了创建批处理脚本以运行maven测试脚本的完全相同(ish)任务。 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. 问题是使用mvn clean install调用maven scrips ...本身就是一个脚本,因此需要使用调用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脚本。

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 .

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. 我不知道你的问题的直接答案,但如果你做了很多这些脚本,那么学习像perl这样更强大的语言可能是值得的。 Free implementations exist for Windows (eg activestate, cygwin). Windows存在免费实现(例如activestate,cygwin)。 I've found it worth the initial effort for my own tasks. 我发现值得为自己的任务做最初的努力。

Edit: 编辑:

As suggested by @Ferruccio, if you can't install extra software, consider vbscript and/or javascript. 正如@Ferruccio所建议的,如果您无法安装额外的软件,请考虑使用vbscript和/或javascript。 They're built into the Windows scripting host. 它们内置于Windows脚本主机中。

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

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. 你需要使用调用的原因是mvn本身是一个批处理文件,批处理文件需要通过调用相互调用,否则控制不会返回给调用者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM