如何回显$?工作?

2024-03-10

我正在编写一些 PowerShell 脚本来执行一些构建自动化。我发现here https://stackoverflow.com/a/4917997/1977871 that echo $?根据先前的语句返回 true 或 false。我刚刚发现 echo 是写输出. 写主机 $?也有效。但我还是不清楚这是怎么回事$?作品。有人可以对此说几句话吗?正在搜索 echo $?网上并没有给我太多。


来补充马丁·布兰德尔的有用回答 https://stackoverflow.com/a/39865555/45375更详细的信息:

tl;dr

  • 自动变量$? (see Get-Help about_Automatic Variables https://technet.microsoft.com/en-us/library/hh847768.aspx)包含一个Boolean这反映了whether 任何非终止的发生了错误 in the 最近的陈述.

    • Since $?是在之后设置的every声明,您必须在利益声明之后立即检查它,或者保存它以供以后检查。
    • 请参阅下文了解潜在的违反直觉的行为。
  • 自动变量$LASTEXITCODE 补充这通过记录特定的退出代码最近执行的外部命令行实用程序(控制台应用程序,例如findstr).

    • $LASTEXITCODE补充$?在那里面$?仅反映abstract外部实用程序成功或失败 - 退出代码0被映射到$True,任何非零退出代码$False- 然而$LASTEXITCODE包含实际的退出代码。
    • Since $LASTEXITCODE仅为外部命令行实用程序设置,其值通常保持有效的时间长于$?,这是在之后设置的every陈述。

有许多微妙之处围绕如何$?已设置,并且其值准确地表示什么:

  • 在 PowerShell 版本中v7.2 之前的版本, $?有一个外部命令行实用程序的误报风险,即它可以报告$false即使当$LASTEXITCODE is 0,即当2>使用重定向并且存在实际的 stderr 输出 - 请参阅这个答案 https://stackoverflow.com/a/66726535/45375了解详情。

  • $?只反映了发生不终止的 errors,因为(更为罕见)终止默认情况下,错误会终止当前命令行/脚本的执行,要处理它们,您需要使用try / catch(首选)或trap(参见Get-Help about_Try_Catch_Finally https://technet.microsoft.com/en-us/library/hh847793.aspx and Get-Help about_Trap https://technet.microsoft.com/en-us/library/hh847742.aspx).

    • 相反,您可以选择使用首选项变量将非终止错误视为终止错误$ErrorActionPreference或通用 cmdlet 参数
      -ErrorAction (alias -EA) - see Get-Help about_Preference_Variables https://technet.microsoft.com/en-us/library/hh847796.aspx and Get-Help about_CommonParameters http://go.microsoft.com/fwlink/?LinkID=113216.
  • 除非明确ignored(与常见的-ErrorAction Ignorecmdlet 参数),所有非终止错误(和捕获的终止错误)都收集在自动$Error收藏,按时间倒序排列;即元素$Error[0]包含最近的错误。

  • 对于命令multiple输入对象被传递,$?含有$False只告诉你处理最后一个输入对象失败。换句话说:可能发生错误任何子集输入对象的数量,包括他们全部.

    • 要确定确切的错误计数和有问题的输入对象,您必须检查$Error收藏。
  • With 非远程间接执行 cmdlets您向其传递要执行的目标命令 - 例如Invoke-Expression, Start-Process and Start-Job and Invoke-Command without the -ComputerName参数(不涉及远程处理 - 见下文)-$?只反映目标命令是否可以被调用原则,无论该命令是否报告错误。

    • 一个简单的例子:Invoke-Expression '1 / 0' sets $? to $True(!), 因为Invoke-Expression能够解析并invoke表达式,即使表达式本身失败了。
    • 再次,检查$Error集合告诉您目标命令是否报告了错误以及报告了哪些错误。
  • With remoting(总是间接执行)cmdlet,特别是与Invoke-Command-ComputerName参数(这是典型的),但也可以使用隐式远程处理 cmdlet,$? does反映是否目标命令报告任何错误。

    • A simple example (must be run from an elevated console and assumes that the local machine is already set up for remoting):
      Invoke-Command -ComputerName . { 1 / 0 }, because remoting is involved, indeed sets $? to $False to reflects the failure of target command 1 / 0.
      Note that even though the local computer (.) is targeted, use of -ComputerName invariably uses remoting.

    • 请注意,根据设计,远程处理通常会报告终止远程发生的错误非终止的可能是这样,一台目标机器上的正常终止错误不会中止所有其他机器上的处理。


  • 确实反映错误的命令示例$?:

      # Invoking a non-existing cmdlet or utility directly.
      NoSuchCmd
    
      # Ditto, via call operator &.
      # Note, however, that using a *script block* with & behaves differently - see below.
      & 'NoSuchCmd'
    
      # Invoking a cmdlet with invalid parameter syntax.
      Get-ChildItem -NoSuchParameter
    
      # Invoking a cmdlet with parameter values that cause a (non-terminating) runtime error.
      Get-ChildItem NoSuchFile
    
      # Invoking an external utility that reports a nonzero exit code. 
      findstr -nosuchoptions
      # The specific exit code is recorded in $LASTEXITCODE, 
      # until the next external utility is called.
    
      # Runtime exceptions
      1 / 0
    
      # A cmdlet that uses remoting:
      # (Must be run from an elevated session, and the local machine must
      # be configured for remoting first - run `winrm quickconfig`).
      # Note that remoting would NOT be involved WITHOUT the -ComputerName parameter, 
      # in which case `$?` would only reflect whether the script block could be
      # _invoked_, irrespective of whether its command(s) then fail or not.
      Invoke-Command -ComputerName . { 1 / 0 }
    
      # A .NET method that throws an exception.
      # Note: Outside of a `try/catch` handler, this is a non-terminating error.
      # Inside a `try/catch` handler, .NET exceptions are treated as terminating
      # and trigger the `catch` block.
      [System.IO.Path]::IsPathRooted('>')
    
  • 不反映错误的命令示例$?:

      <#
        Non-remoting indirect execution cmdlets:
    
        $? reflects only whether the specified command could be 
        *invoked*, irrespective of whether the command itself then failed or not.
    
        In other words: $? is only $False if the specified command could not even be
        executed, such as due to invalid parameter syntax, an ill-formed target
        command, or a missing target executable. 
    
      #>
    
      # Invoking a command stored in a script block.
      & { 1 / 0 }
    
      # Invoking an expression stored in a string.
      Invoke-Expression '1 / 0'
    
      # Starting a background job.
      Start-Job { 1/ 0 }
    
      # The *non-remoting* form of Invoke-Command (WITHOUT -ComputerName).
      Invoke-Command { 1 / 0 }
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何回显$?工作? 的相关文章

随机推荐

  • Apache 模块命令解析器原型

    我正在创建 Apache2 模块并遇到奇怪的编译问题 这是我的函数的原型 用于解析名为 的配置命令分析IP static const char apr cfg set analytics ip cmd parms cmd void conf
  • tensorflow conv2d内存消耗解释?

    output tf nn conv2d input weights strides 1 3 3 1 padding VALID My input形状为 200x225x225x1 weights是 15x15x1x64 因此 output形
  • nbactions.xml 有何用途?

    经过一段时间的搜索后 我认为这与使用 Maven 和 Net beans 构建应用程序有关 但我似乎找不到这方面的良好文档 使用 nbaction xml 可以实现哪个目标 如果有的话 哪一个与 Eclipse 等效 The nbactio
  • 为什么这个 constexpr 代码会导致 GCC 吃掉我所有的 RAM?

    以下程序将调用fun2 MAXD 1 次 不过 最大递归深度永远不应该超过 MAXD 如果我的想法是正确的 因此 编译可能需要一些时间 但它不应该占用我的内存 include
  • 特定文件夹结构中的文件的 Azure 存储

    目前我有一些 ftp 其中有一些文件夹和文件的深层结构 它甚至可能比根文件夹低 10 级 由于我已经成功地将本地数据库迁移到 azure 数据库 我还想知道是否有任何 azure ftp 我也可以用来迁移它 我知道我们有类似 Azure 存
  • 如何在 Bootstrap 3 中使用 bootstrap-theme.css?

    从以下位置下载完整的 bootstrap 3 包后http getbootstrap com http getbootstrap com 我注意到主题有一个单独的 css 文件 如何利用它 请解释 包括我bootstrap theme cs
  • 创建一个128字节的随机数

    If the rand 函数创建一个长度为 4 字节的随机数 我想创建一个长度为 1024 位 128 字节 的随机数 这是通过连接来获得此值的最简单方法rand 函数256次还是有其他方法 include
  • Random().Next() 的流需要多长时间才会重复?

    考虑 NETRandom stream var r new Random while true r Next 重复需要多长时间 根据文档 伪随机数是从有限个数中以相等的概率选择的 一组数字 选定的数字是 不是完全随机的 因为 确定的数学算法
  • 检查 pandas 列中的连续行值

    I have hi 0 1 1 2 2 4 3 8 4 3 5 3 6 2 7 8 8 3 9 5 10 4 我有一个列表和单个整数的列表 如下所示 2 8 3 2 2 8 对于主列表中的每个项目 我想找出它第一次出现在列中的索引 因此 对
  • 使用 node.js 的 http 请求失败 发送后无法设置标头

    我尝试使用 https http 请求服务器并将结果显示在网页中 它作为服务器上的脚本工作 但由于我通过 get 请求返回结果而失败 var express require express var app express var port
  • Ember Data:在控制台中获取模型

    我有最简单的 Ember 应用程序JSBin http jsbin com aYIkAcUk 2 edit 我想做的就是找到一个模型 基于其他所以问题 https stackoverflow com questions 18756092 h
  • 跨域AJAX post调用

    我必须对位于另一台服务器上的 asp 表单进行 POST 调用 带参数 对于开发 我在同一台服务器上执行了此操作 并且运行良好 但现在我在另一台服务器上测试它 我收到的不是 200 状态 而是 0 状态 我认为这是因为它是跨域 AJAX 调
  • 如何知道三星 S8、S8+、S9 等底部导航栏何时可见?

    三星 S8 S8 S9 等上的底部导航栏在关闭时会导致 UI 和动画噩梦 导致视图从应用程序的顶部和底部移入和移出 对于这些设备 如果导航栏打开 一切都会完美运行 但如果关闭 所有动画都会低于导航栏的高度 我的想法是调整动画 但是 我很难弄
  • Paper_trail 宝石能力

    我想知道是否可以使用以下用例来实现纸迹 https github com airblade paper trail宝石 维基百科类型的应用程序 其中登录用户可以更改 编辑维基页面 其中 版主可以撤消特定更改 我知道 papertrail 允
  • 如何查看TortoiseSVN中的所有修订?

    TortoiseSVN 显示日志 选项按日期过滤修订 手动更改这些日期很麻烦 如何以最少的麻烦查看所有修订 You must在存储库的根文件夹中执行此操作 取消选中 复制 重命名时停止 Check Include merged revisi
  • 如何在android中离线获取纬度和经度?

    我想在 WiFi 和 Gps 关闭时获取当前位置 纬度和经度 可以从移动 SIM 网络获取纬度和经度 我在谷歌上搜索了更多 但没有得到满意的答案 从我昨天的经验来看question https stackoverflow com q 220
  • 简单地显示 UIInterpolatingMotionEffect 的值?

    这是一个谜题 想象一个典型的 UIInterpolatingMotionEffect UIInterpolatingMotionEffect horizontalMotionEffect UIInterpolatingMotionEffec
  • 使用 FluentValidation 的 WithMessage 方法和命名参数列表

    我正在使用 FluentValidation 并且想使用对象的某些属性值来格式化消息 问题是我对 C 中的表达式和委托的经验很少 FluentValidation 已经提供了一种使用格式参数来执行此操作的方法 RuleFor x gt x
  • Python-检查字符串是否包含数字[重复]

    这个问题在这里已经有答案了 我正在制作一个函数 它使用 while True 循环来要求用户输入通过条件的密码 长度最少为 8 15 个字符 并且至少包含一个整数 我对如何正确检查整数的输入感到困惑 我的程序 def enterNewPas
  • 如何回显$?工作?

    我正在编写一些 PowerShell 脚本来执行一些构建自动化 我发现here https stackoverflow com a 4917997 1977871 that echo 根据先前的语句返回 true 或 false 我刚刚发现