0) { $args[0] } else { Read-Host 'Action' -6ren">
gpt4 book ai didi

string - PowerShell - "Ternary"If 语句后的大写字母

转载 作者:行者123 更新时间:2023-12-03 00:50:42 25 4
gpt4 key购买 nike

在 PowerShell 中执行此操作的正确方法是什么?

$action = if ($args.Length > 0) { $args[0] } else { Read-Host 'Action' } #.ToUpper()
echo $action

以下似乎是代码异味

$action = if ($args.Length > 0) { $args[0] } else { Read-Host 'Action' }
$action = $action.ToUpper()
echo $action

最佳答案

您拥有的第一个代码块几乎可以像编写的那样工作(您可以分配 if/else 语句的结果)。

$action = $(if ($args.Length -gt 0) { $args[0] } else { Read-Host 'Action' }).ToUpper()

您只需要使用 -gt (大于)运算符而不是 > , 并用括号括起来。

关于string - PowerShell - "Ternary"If 语句后的大写字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28355968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com