为什么 Powershell 用空格替换逗号?

发布于 2024-12-10 04:21:28 字数 749 浏览 1 评论 0原文

我正在 Powershell 中编写一些脚本来自动执行任务。该脚本将获取参数,例如:

PS > myscript.ps1 par=a,1,2,0.1 par=b,3,4,0.1 par=c,5,6,0.1 bin=file.exe exeargs="fixed args for file.exe"

简而言之,file.exe 是一个接受参数(包括 a、b 和 c)的可执行文件,并且此 ps1 脚本将执行 file .exe 传递指定范围内的参数 abc,并按指定精度改变它们。

问题是,我首先用字符“=”分割$args中的每个$arg,然后我应该用“,”分割它们以获得指定的值。

问题是,当我这样做时:

foreach ($arg in $args)
{
    $parts = ([string] $arg).split("=")
    Write-Host $parts[1]
}

输出是

a 1 2 0.1
b 3 4 0.1
c 5 6 0.1
file.exe
fixed args for file.exe

即,它已经用空格替换了“,”字符,所以我的第二个分割应该是空格,而不是逗号。

你猜为什么会发生这种情况吗?

提前致谢!

I'm doing some script in Powershell to automate a task. This script is going to get arguments, such as:

PS > myscript.ps1 par=a,1,2,0.1 par=b,3,4,0.1 par=c,5,6,0.1 bin=file.exe exeargs="fixed args for file.exe"

In short, file.exe is an executable which accept parameters (including a, b and c) and this ps1 script is going to execute file.exe passing args a, b and c within the specified range, varying 'em by the specified precision.

The question is, I first split each $arg in $args by the character "=", and then I should split them by "," to get the specified values.

The thing is, when I do:

foreach ($arg in $args)
{
    $parts = ([string] $arg).split("=")
    Write-Host $parts[1]
}

The output is

a 1 2 0.1
b 3 4 0.1
c 5 6 0.1
file.exe
fixed args for file.exe

I.e., it already substituted the "," character with a whitespace, so my second split should be with white space, not with comma.

Any guess on why does it happen?

Thanks in advance!

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

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

发布评论

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

评论(2

霓裳挽歌倾城醉 2024-12-17 04:21:28

首先,为什么你要把它写成 C 程序之类的?当 Powershell 具有更强大的参数概念时,您不必传递这样的参数,使用 $args 并拆分 = 等。参数和参数,而不是进行您正在执行的解析。 (有关参数的更多信息,请参见:http://technet.microsoft.com/en-us /library/dd315296.aspx

话虽如此,让我回答你的问题:

你正在做的是当你传入如下参数时:

par=a,1,2,0.1 par=b,3,4,0.1 par=c,5,6,0.1 bin=file.exe exeargs="fixed args for file.exe"

您正在传递数组数组。第一个元素是包含元素的数组:

par=a
1
2
0.1

好的,进行拆分:

当您执行 [string] $a 时,您正在将数组转换为字符串。默认情况下,这意味着包含元素 1,2,3 的数组将变为 1 2 3

因此,您的第一个参数 par=a,1,2,0.1 变为 par=a 1 2 0.1 并且 = 上的拆分意味着 < code>parts[1] 变为 a 1 2 0.1,这就是您所看到的。

那么如何保留逗号呢?

只需将数组转换为字符串,中间用 , 代替空格,对吧?

以下是一些方法:

使用 -join 运算符:

foreach ($arg in $args)
{

    $parts = ($arg -join ",").split("=")
    Write-Host $parts[1]
}

现在您将看到带有所需逗号的输出。

另一种方法是使用 $ofs 特殊变量:(

foreach ($arg in $args)
{
    $ofs =","
    $parts = ([string] $arg).split("=")
    Write-Host $parts[1]
}

更多关于 $ofs 的信息:http://blogs.msdn.com/b/powershell/archive/2006/07/15/what-is-ofs.aspx

免责声明 - 所有这些解释都是为了让您了解正在发生的事情。请不要继续此操作并使用参数。

First of all why are you writing it like a C program or something? You don't have to pass arguments like that, use $args and split on = etc. when Powershell has a more powerful concept of parameters, whereby you can pass the named paramters and arguments rather than doing the parsing that you are doing. ( More on parameters here: http://technet.microsoft.com/en-us/library/dd315296.aspx)

With that said, let me answer your question:

What you are doing is when you pass in arguments like:

par=a,1,2,0.1 par=b,3,4,0.1 par=c,5,6,0.1 bin=file.exe exeargs="fixed args for file.exe"

you are passing in array of arrays. The first element is the array with elements:

par=a
1
2
0.1

Ok coming to the split:

When you do [string] $a, you are converting the array into a string. By default this means an array with elements 1,2,3 will become 1 2 3.

So your first argument there par=a,1,2,0.1, becomes par=a 1 2 0.1 and the split on = means parts[1] becomes a 1 2 0.1, which is what you see.

So how can you retain the comma?

Just make an array to be converted into a string with , inbetween than space, right?

Here are some ways to do that:

Using -join operator:

foreach ($arg in $args)
{

    $parts = ($arg -join ",").split("=")
    Write-Host $parts[1]
}

now you will see the output with the commas that you want.

Another way, using $ofs special variable:

foreach ($arg in $args)
{
    $ofs =","
    $parts = ([string] $arg).split("=")
    Write-Host $parts[1]
}

(more on $ofs here: http://blogs.msdn.com/b/powershell/archive/2006/07/15/what-is-ofs.aspx )

Disclaimer - All this explanation to make you understand what is happening. Please do not continue this and use paramters.

好菇凉咱不稀罕他 2024-12-17 04:21:28

这是在解析脚本的命令行时发生的,而不是在 split() 方法期间发生的。要看到这一点,请尝试在开头放置“Write-Host $args”,如下所示:

Write-Host $args
foreach ($arg in $args)
{
    $parts = ([string] $arg).split("=")
    Write-Host $parts[1]
}

这是因为“,”字符用于分隔数组中的元素。例如:

PS C:\temp> $a = 1,2,3
PS C:\temp> Write-Host $a
1 2 3
PS C:\temp> $a.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

尝试使用以下命令行:

.\myscript.ps1 "par=a,1,2,0.1" "par=b,3,4,0.1" "par=c,5,6,0.1" "bin=file.exe" "exeargs=fixed args for file.exe"

This is happening in the parsing of the command line for your script and not during the split() method. To see this, try putting a "Write-Host $args" at the beginning, like so:

Write-Host $args
foreach ($arg in $args)
{
    $parts = ([string] $arg).split("=")
    Write-Host $parts[1]
}

This is because the ',' character is used to separate elements in an array. For example:

PS C:\temp> $a = 1,2,3
PS C:\temp> Write-Host $a
1 2 3
PS C:\temp> $a.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

Try this command line instead:

.\myscript.ps1 "par=a,1,2,0.1" "par=b,3,4,0.1" "par=c,5,6,0.1" "bin=file.exe" "exeargs=fixed args for file.exe"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文