我可以得到“&"吗?或“-和"在 PowerShell 中工作? [英] Can I get "&&" or "-and" to work in PowerShell?
问题描述
&&
在 Google 搜索上是出了名的难搜索,但我找到的最好的是 这篇文章 说使用 -and
.
&&
is notoriously hard to search for on Google Search, but the best I've found is this article which says to use -and
.
不幸的是,它没有提供更多信息,而且我无法找到我应该用 -and
做什么(同样,这是众所周知的难以搜索的东西).
Unfortunately it doesn't give any more information, and I can't find out what I'm supposed to do with -and
(again, a notoriously hard thing to search for).
我试图在其中使用它的上下文是执行 cmd1,如果成功,则执行 cmd2",基本上是这样的:
The context I'm trying to use it in is "execute cmd1, and if successful, execute cmd2", basically this:
csc /t:exe /out:a.exe SomeFile.cs && a.exe
如果您只想在一行上运行多个命令,并且不关心第一个命令是否失败,您可以使用 ;
对于我的大多数目的,这很好.
If you just want to run multiple commands on a single line and you don't care if the first one fails or not, you can use ;
For most of my purposes this is fine.
例如:kill -n myapp;./myapp.exe
.
推荐答案
在 CMD 中,'&&'表示执行命令1,如果成功,执行命令2".我已经将它用于以下方面:
In CMD, '&&' means "execute command 1, and if it succeeds, execute command 2". I have used it for things like:
build && run_tests
在 PowerShell 中,您可以做的最接近的事情是:
In PowerShell, the closest thing you can do is:
(build) -and (run_tests)
它具有相同的逻辑,但是命令的输出文本丢失了.不过,也许它对你来说已经足够了.
It has the same logic, but the output text from the commands is lost. Maybe it is good enough for you, though.
如果您在脚本中执行此操作,最好将语句分开,如下所示:
If you're doing this in a script, you will probably be better off separating the statements, like this:
build
if ($?) {
run_tests
}
2019/11/27:&&
运算符现在可用于 PowerShell 7 Preview 5+:
2019/11/27: The &&
operator is now available for PowerShell 7 Preview 5+:
PS > echo "Hello!" && echo "World!"
Hello!
World!
这篇关于我可以得到“&"吗?或“-和"在 PowerShell 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!