我想在Windows CMD控制台中运行两个命令。
在Linux中我会这样做
touch thisfile ; ls -lstrh
在Windows上是怎么做的呢?
我想在Windows CMD控制台中运行两个命令。
在Linux中我会这样做
touch thisfile ; ls -lstrh
在Windows上是怎么做的呢?
当前回答
尝试用这些行创建一个.bat或.cmd文件,使用doskey和$T,相当于&,在一行中执行多个命令行:
touch=echo off $T echo. ^> $* $T dir /B $T echo on
它将创建一个空文件。
例子:
touch myfile
在cmd中,你会得到这样的东西:
但是正如前面提到的,建议使用&操作符从CMD提示符在一行中执行多个命令行。
喜欢=)
其他回答
当您尝试在一行中使用或操作变量时,请注意它们的内容!例如,如下所示的变量
PATH=C:\程序文件(x86)\某处;“C: \企业\酷工具”;% USERPROFILE % \ AppData \当地\ \ WindowsApps微软;
可能会导致很多无法处理的麻烦,如果你使用它作为%PATH%
右括号终止组语句 双引号不允许使用%PATH%来处理圆括号问题 像%USERPROFILE%这样的引用变量将包含什么?
文档中的一段话:
来源:微软,Windows XP专业产品文档,命令shell概述 另外:Windows CMD命令的A-Z索引
Using multiple commands and conditional processing symbols You can run multiple commands from a single command line or script using conditional processing symbols. When you run multiple commands with conditional processing symbols, the commands to the right of the conditional processing symbol act based upon the results of the command to the left of the conditional processing symbol. For example, you might want to run a command only if the previous command fails. Or, you might want to run a command only if the previous command is successful. You can use the special characters listed in the following table to pass multiple commands. & [...] command1 & command2 Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command. && [...] command1 && command2 Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully. || [...] command1 || command2 Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero). ( ) [...] (command1 & command2) Use to group or nest multiple commands. ; or , command1 parameter1;parameter2 Use to separate command parameters.
您可以使用&来一个接一个地运行命令。示例:c:\dir & vim myFile.txt
我尝试在同一个窗口中有两个ping,它是在同一行上的串行命令。完成第一个命令后,执行第二个命令。
解决方案是在Windows 7命令提示符中结合start /b。
像往常一样启动,不带/b,并在单独的窗口中启动。
在同一行中用于启动的命令是:
start /b command1 parameters & command2 parameters
无论如何,如果您希望解析输出,我不建议使用这种方法。 我注意到输出在命令的输出之间被打乱了。
&是Bash中等价的;&&是Bash中的&&(只在前一个没有导致错误时才运行命令)。