gpt4 book ai didi

powershell - Powershell中的命令行参数

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

因此,问题在于,当我运行基本脚本以简单反射(reflect)命令行中传递的内容时,参数并没有按照我期望的方式分开。

基本代码是:

write-host "`$args`[0`] = $args[0]"
write-host "`$args`[1`] = $args[1]"
write-host "`$args`[2`] = $args[2]"

如果我将脚本称为
./script apples oranges bananas

我懂了
$args[0] = apples oranges bananas[0]
$args[1] = apples oranges bananas[1]
$args[2] = apples oranges bananas[2]

如果重要的话,我将在Powershell 2.0中执行此操作

最佳答案

您需要像这样将变量包装到$(..)中:

write-host "`$args`[0`] = $($args[0])"
write-host "`$args`[1`] = $($args[1])"
write-host "`$args`[2`] = $($args[2])"

这适用于不是简单标量变量的任何表达式:
$date = get-date
write-host "day: $($date.day)"
write-host "so web page length: $($a = new-object Net.WebClient; $a.DownloadString('http://stackoverflow.com').Length)"
write-host "$(if (1 -eq (2-1)) { 'is one' } else {'is not'} )"

关于powershell - Powershell中的命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3385052/

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