在 Windows PowerShell 中有条件地执行进程(例如 Bash 中的 && 和 || 运算符)

发布于 2024-08-14 12:59:17 字数 748 浏览 3 评论 0原文

我想知道是否有人知道一种根据前一个程序的退出成功/失败有条件地执行程序的方法。如果program1成功退出而不测试LASTEXITCODE变量,有什么方法可以在program1之后立即执行program2吗?我尝试了 -band 和 -and 运算符,但没有效果,尽管我有一种感觉它们无论如何都不起作用,最好的替代品是分号和 if 语句的组合。我的意思是,当涉及到在 Linux 上从源代码自动构建软件包时, &&操作员无法被打败:

# Configure a package, compile it and install it
./configure && make && sudo make install

PowerShell 会要求我执行以下操作,假设我实际上可以在 PowerShell 中使用相同的构建系统:

# Configure a package, compile it and install it
.\configure ; if ($LASTEXITCODE -eq 0) { make ; if ($LASTEXITCODE -eq 0) { sudo make install } }

当然,我可以使用多行,将其保存在文件中并执行脚本,但想法是使其简洁(节省击键)。也许这只是 PowerShell 和 Bash 之间的差异(甚至是支持 && 运算符的内置 Windows 命令提示符),我需要适应,但如果有更干净的方法来做到这一点,我会很乐意知道。

I'm wondering if anybody knows of a way to conditionally execute a program depending on the exit success/failure of the previous program. Is there any way for me to execute a program2 immediately after program1 if program1 exits successfully without testing the LASTEXITCODE variable? I tried the -band and -and operators to no avail, though I had a feeling they wouldn't work anyway, and the best substitute is a combination of a semicolon and an if statement. I mean, when it comes to building a package somewhat automatically from source on Linux, the && operator can't be beaten:

# Configure a package, compile it and install it
./configure && make && sudo make install

PowerShell would require me to do the following, assuming I could actually use the same build system in PowerShell:

# Configure a package, compile it and install it
.\configure ; if ($LASTEXITCODE -eq 0) { make ; if ($LASTEXITCODE -eq 0) { sudo make install } }

Sure, I could use multiple lines, save it in a file and execute the script, but the idea is for it to be concise (save keystrokes). Perhaps it's just a difference between PowerShell and Bash (and even the built-in Windows command prompt which supports the && operator) I'll need to adjust to, but if there's a cleaner way to do it, I'd love to know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假装不在乎 2024-08-21 12:59:17

您可以创建一个函数来执行此操作,但据我所知,没有直接的方法可以执行此操作。

function run-conditionally($commands) {
   $ranAll = $false
   foreach($command in $commands) {
      invoke-command $command
      if ($LASTEXITCODE -ne 0) {
          $ranAll = $false
          break; 
      }
      $ranAll = $true
   }

   Write-Host "Finished: $ranAll"

   return $ranAll
}

然后调用它类似于

run-conditionally(@(".\configure","make","sudo make install"))

There might be a someErrors There 这是即兴的,没有方便的 powershell 环境。

You could create a function to do this, but there is not a direct way to do it that I know of.

function run-conditionally($commands) {
   $ranAll = $false
   foreach($command in $commands) {
      invoke-command $command
      if ($LASTEXITCODE -ne 0) {
          $ranAll = $false
          break; 
      }
      $ranAll = $true
   }

   Write-Host "Finished: $ranAll"

   return $ranAll
}

Then call it similar to

run-conditionally(@(".\configure","make","sudo make install"))

There are probably a few errors there this is off the cuff without a powershell environment handy.

梦旅人picnic 2024-08-21 12:59:17

我也因为缺少 && 而感到非常痛苦,所以根据 GrayWizardX 的答案编写了以下简单脚本(不能按原样工作):

foreach( $command in $args )
{
  $error.clear()
  invoke-command $command
  if ($error) { break; }
}

如果将其保存为 rc.ps1 (用于“有条件运行” )在路径中的目录中,您可以像这样使用它:

rc {.\configure} {make} {make install}

使用脚本块(大括号)作为参数而不是字符串意味着您可以在键入命令时使用制表符补全,这要好得多。该脚本几乎与 && 一样好,并且有效。

I was really hurting for the lack of &&, too, so wrote the following simple script based on GrayWizardX's answer (which doesn't work as-is):

foreach( $command in $args )
{
  $error.clear()
  invoke-command $command
  if ($error) { break; }
}

If you save it as rc.ps1 (for "run conditionally") in a directory in your path, you can use it like:

rc {.\configure} {make} {make install}

Using script blocks (the curly braces) as arguments instead of strings means you can use tab completion while typing out the commands, which is much nicer. This script is almost as good as &&, and works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文