为什么 Powershell 用空格替换逗号?
我正在 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
传递指定范围内的参数 a
、b
和 c
,并按指定精度改变它们。
问题是,我首先用字符“=”分割$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,为什么你要把它写成 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"
您正在传递数组数组。第一个元素是包含元素的数组:
好的,进行拆分:
当您执行
[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
运算符:现在您将看到带有所需逗号的输出。
另一种方法是使用
$ofs
特殊变量:(更多关于 $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:
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 elements1,2,3
will become1 2 3
.So your first argument there
par=a,1,2,0.1
, becomespar=a 1 2 0.1
and the split on=
meansparts[1]
becomesa 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:now you will see the output with the commas that you want.
Another way, using
$ofs
special variable:(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.
这是在解析脚本的命令行时发生的,而不是在 split() 方法期间发生的。要看到这一点,请尝试在开头放置“Write-Host $args”,如下所示:
这是因为“,”字符用于分隔数组中的元素。例如:
尝试使用以下命令行:
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:
This is because the ',' character is used to separate elements in an array. For example:
Try this command line instead: