简体   繁体   English

在Windows命令行上转义字符串

[英]escaping string on Windows command line

I'm trying to pass string to Win32 program from command line so it will be printed without changes. 我正在尝试从命令行将字符串传递给Win32程序,因此它将在不更改的情况下打印。

Why I have to escape 为什么我要逃避
"AAA <BBB@pobox.com>" as """AAA <BBB@pobox.com>""" “AAA <BBB@pobox.com>”“”“AAA <BBB@pobox.com>”“”
but
"AAA <BBB@pobox.com>", (comma included) as "\\"AAA ^<BBB@pobox.com^>\\"," “AAA <BBB@pobox.com>”, (逗号包括)为“\\”AAA ^ <BBB@pobox.com ^> \\“,”

I see no consistency in escaping rules for windows command line 我发现Windows命令行的转义规则没有一致性

PS I'm trying to generate a .cmd file PS我正在尝试生成.cmd文件

Update: 更新:

I'm using simple C program for testing that is compiled with gcc, no additional object files linked. 我正在使用简单的C程序进行测试,使用gcc编译,没有其他对象文件链接。 If I replace it with perl, rules remain same. 如果我用perl替换它,规则保持不变。

I'm trying to create a general escaping algorithm. 我正在尝试创建一个通用的转义算法。 It will generate .cmd file which will call perl with output redirect. 它将生成.cmd文件,该文件将使用输出重定向调用perl。 Currently I have a problem that if string contains odd number of double quotes which are escaped with backslash, output redirect does not function. 目前我有一个问题,如果字符串包含使用反斜杠转义的奇数双引号,则输出重定向不起作用。 Same problem is described in the last comment to http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx . http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx的最新评论中描述了同样的问题。

If I use "" as escape for ", it splits on space, so it will result it 2 parameters instead of one. Also "" has some artifacts. 如果我使用“”作为“它的转义”,它会在空格上分割,因此它将导致2个参数而不是一个。此外,“”还有一些工件。

In windows there is no one way of getting a command line and parsing it. 在Windows中,没有一种方法可以获取命令行并解析它。 Mostly programs have generally been left to deal with that themselves. 一般来说,程序通常都是自己处理的。

There is a recent post by Raymond Chen about the CommandLineToArgvW function which mentions various rules about quoting but they'll only apply if the program uses that particular function. 最近Raymond Chen发表了一篇关于CommandLineToArgvW函数的文章,其中提到了有关引用的各种规则,但它们仅在程序使用该特定函数时才适用。 http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx

In windows the command line is passed to the program unmolested (ie no wildcards expanded) and then the program needs to deal with it. 在Windows中,命令行被无故障地传递给程序(即没有扩展通配符),然后程序需要处理它。 The programming language may provide a convenience which does some default argument parsing, and this might use a standard windows function like CommandLineToArgvW but even so the program could opt to read the unadulterated string itself thereby skipping those standards. 编程语言可以提供一些方便,可以进行一些默认的参数解析,这可能会使用像CommandLineToArgvW这样的标准Windows函数,但即使如此,程序也可以选择读取未掺杂的字符串本身,从而跳过这些标准。

This means you need to figure out the rules for the particular program you are trying to script yourself and then use them. 这意味着您需要找出自己尝试编写脚本的特定程序的规则,然后再使用它们。

I've just tried those as parameters into one of my own programs, and both versions (with or without the comma) can be escaped in both ways (using either """ or \\" to escape the quotes). 我刚刚尝试将这些参数作为我自己的程序中的一个,并且两个版本(带或不带逗号)都可以通过两种方式进行转义(使用"""\\"来转义引号)。 The only reason I can see that the < and > need to be escaped with ^ in the second version is that as the command line is seeing them as I/O redirections prior to passing them to the application, due to the different way of escaping the string quotes. 我可以看到<>需要在第二个版本中使用^进行转义的唯一原因是,由于转发方式不同,命令行在将它们传递给应用程序之前将其视为I / O重定向字符串引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM