conditional-operator - 三元比较输出新值

标签 conditional-operator powershell-7.0

PS C:\Users\Scott> $openlog = "YES"
PS C:\Users\Scott> ($openlog -eq "YES") ? ($openlog = "NO") : ($openlog = "YES")
NO
PS C:\Users\Scott> ($openlog -eq "YES") ? ($openlog = "NO") : ($openlog = "YES")
YES
PS C:\Users\Scott> ($openlog -eq "YES") ? ($openlog = "NO") : ($openlog = "YES")
NO
PS C:\Users\Scott> ($openlog -eq "YES") ? ($openlog = "NO") : ($openlog = "YES")
YES
PS C:\Users\Scott>

这有效,即它会切换值,但也会将其写入控制台。为什么?如何在不使用 | 的情况下正确执行此操作并且不输出新值输出为空

最佳答案

您可以使用下面的代码来设置变量,而无需输出到控制台。

$openlog = $openlog -eq "是"? “否”:"is"

我还不知道为什么你的示例输出到控制台。我浏览了implementation这是一个从未处理过并且在测试期间也没有处理的用例。

更新:正如下面 mklement0 提到的

enclosing an assignment in (...) passes the value being assigned through

关于conditional-operator - 三元比较输出新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68641111/

相关文章:

powershell - PS7.1-如何将管道链接与自定义功能一起使用?

javascript - 是否有像条件运算符一样缩短 if 语句的语法?

Azure 函数 | PowerShell |无法加载类型 'System.Security.Cryptography.SHA256Cng'

Java 三元(内联)运算符 : accesing comparation variables

swift - 为什么 Swift 的三元运算符对空格如此挑剔?

json - 从 powershell 中的 JSON 对象中删除一个属性

azure-powershell - PowerShell 7. ForEach-Object -Parallel 不会针对 Azure PowerShell 进行身份验证

powershell - 为什么 `Install-Module` 不遵守脚本范围内的 $ErrorActionPreference=Stop ?

java - 地址簿中的逻辑问题,不应有空白条目或相同条目...请帮助

c++ - C 和 C++ 之间的条件运算符差异