逗号分隔输入参数? [英] Comma separate input parameters?

查看:116
本文介绍了逗号分隔输入参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本 foo.cmd

echo %1 %2



在PowerShell中,我运行:

In PowerShell I run:

foo.cmd "a,b" "c"

预期输出: a,bc

实际输出: ab

为什么?

推荐答案

PowerShell解析命令行并传递后,双引号被删除到CMD执行,所以CMD实际上看到一个语句

The double quotes are removed after PowerShell parsed the command line and passes it to CMD for execution, so CMD actually sees a statement

foo.cmd a,b c

由于逗号是CMD的分隔符字符该语句等同于

Since the comma is one of CMD's delimiter characters that statement is equivalent to

foo.cmd a b c

为避免此行为,您需要确保在将参数传递给CMD时保留双引号。有几种方法来实现这一点。例如,你可以把单引号的双引号参数:

To avoid this behavior you need to ensure that the double quotes are preserved when passing the arguments to CMD. There are several ways to achieve this. For instance, you could put the double quoted arguments in single qoutes:

foo.cmd '"a,b"' "c"

并将批处理脚本中的位置参数更改为%〜1 %〜2 ,以便CMD从参数中删除双引号。

and change the positional parameters in the batch script to %~1, %~2 so that CMD removes the double quotes from the arguments.

您有PowerShell v3或更高版本,您可以使用 magic parameter - %以避免嵌套引号:

If you have PowerShell v3 or newer you can use the "magic parameter" --% to avoid the nested quotes:

foo.cmd --% "a,b" "c"

%〜1 %〜2

这篇关于逗号分隔输入参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆